zoukankan      html  css  js  c++  java
  • 钉钉开发中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, 0, 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等就不多说,官网文档很全面。

  • 相关阅读:
    怎么获取pm2.5数据----pm2.5 的获取 java 通过url获取后,得到json 格式,在解析json
    MVC介绍
    如何通过URL获取天气PM2.5的数据,返回json
    23种设计模式
    xxx系统的6大质量属性战术
    作业04.提升系统性能
    淘宝网的质量属性分析
    架构漫谈读后感
    软件架构师工作流程----装修与软件的联系
    软件构架实践阅读笔记五(读后感)
  • 原文地址:https://www.cnblogs.com/xiangzhong/p/5753118.html
Copyright © 2011-2022 走看看