云之家,调用发送消息接口报错,求案例
金蝶云社区-anchen_kd
anchen_kd
0人赞赏了该文章 2,015次浏览 未经作者许可,禁止转载编辑于2015年05月05日 17:27:14

发送消息,调用接口一直不成功,C#代码如下:

按钮点击事件:
protected void btnFs_Click(object sender, EventArgs e)
{
//公共号密钥验证规则pubtoken=sha(no,pub,公共号.密钥pubkey,nonce,time)
string pubtoken = SHA1_Hash("498795XT-de1ca401-1d32-4d43-85e2-1be5e9703f88c3d040a8148de356d619b80f98f852d41234561395460000");
string sUrl = "http://im.kdweibo.com/pubacc/pubsend";
string Data = "{\"from\":{\"no\": \"498795\",\"pub\": \"XT-de1ca401-1d32-4d43-85e2-1be5e9703f88\",\"time\": \"1395460000\", \"nonce\": \"139546\",\"pubtoken\": \""+ pubtoken +"\"},\"to\": [{ \"no\": \"498795\", \"code\": \"all\" }],\"type\": 2,\"msg\": { \"text\": \"测试\" }}";

this.txtUrl.Value = GetResponseData(sUrl, Data);
}

哈希方法:
public string SHA1_Hash(string str_sha1_in)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
//str_sha1_out = str_sha1_out.Replace("-", "");
return str_sha1_out;
}
post方法:
public string GetResponseData(string Url,string JSONData)
{
byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
//request.ContentLength = bytes.Length;
request.ContentLength = 0;
request.ContentType = "text/xml";
Stream reqstream = request.GetRequestStream();
//reqstream.Write(bytes, 0, bytes.Length);

//声明一个HttpWebRequest请求
request.Timeout = 90000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.UTF8;

StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strResult = streamReader.ReadToEnd();
streamReceive.Dispose();
streamReader.Dispose();

return strResult;
}

报错信息:

请总部同事协助解决一下,如何调用发关消息接口。