釘釘開發中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等就不多說,官網文檔很全面。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM