钉钉开发中post异步调用问题

  最近项目上在做钉钉开发中,经常会遇到使用post方式调用钉钉内部的方法(微信也有一样),这里涉及到跨域的post调用,但跨域一般都是用jsonp格式,而这个格式只支持get方式。尝试了挺多方法都没有返回

{"errcode":43002,"errmsg":"需要POST请求"}

  让人很费解,用js方式不行,只能尝试从后台解决问题,然后写了如下方法:

/// <summary>
///
/// </summary>
/// <param name="postUrl">post地址</param>
/// <param name="paramData">参数</param>
/// <param name="dataEncode">数据格式</param>
/// <returns></returns>
public static string HttpPost(string postUrl, string paramData, Encoding dataEncode)
{
string ret = string.Empty;
try
{
byte[] byteArray = dataEncode.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/json; charset=utf-8"; webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, , byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return ret;
}

测试了下还真行,所以记录下。前面的建立连接,获取access_token等就不多说,官网文档很全面。

上一篇:jquery中ajax异步调用接口


下一篇:PHP中实现异步调用多线程程序代码