c# https 踩到的坑

到底是學藝不精哈。最近有點流年不利。總是遇到莫名其妙的坑。https發送post請求,死活返回的都是500(內部服務器錯誤)。鬱悶ing。代碼調試也沒有提示啥有效的錯誤提示。用抓包工具也看不出任何異常。只有postman能調通,postman是何方神聖?爲啥總能調通。期初還以爲是證書之類的問題,在代碼中添加了證書之類的東西也還是不行,依然不能解決問題。在google上查了半天,沒有一個人遇到過我的問題麼?鬱悶ing.最後死馬當成活馬醫,在代碼中加了這麼一段:

 request.Credentials = CredentialCache.DefaultCredentials;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

我現在依然是懵逼的狀態。好歹問題是解決了。現在還不知道原理是啥。最近有點忙,留待日後再來理會,或者哪位高人看到,幫我指點一二,在此,不勝感激。哈哈。以下是完整的代碼,僅供參考。哈哈。

 public static string Post(string strUrl, string strParam)
        {

            ServicePointManager.DefaultConnectionLimit = 100;
            ServicePointManager.Expect100Continue = false;

            Stream myResponseStream = null;
            StreamReader myStreamReader = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                string strURL = strUrl;
                request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                request.Credentials = CredentialCache.DefaultCredentials;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Timeout = 10000;
                string paraUrlCoded = strParam;
                byte[] payload;
                payload = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(paraUrlCoded);
                request.ContentLength = payload.Length;
                myResponseStream = request.GetRequestStream();
                myResponseStream.Write(payload, 0, payload.Length);
                myResponseStream.Close();
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream s;
                s = response.GetResponseStream();
                string StrDate = "";
                string strValue = "";
                myStreamReader = new StreamReader(s, Encoding.GetEncoding("UTF-8"));
                while ((StrDate = myStreamReader.ReadLine()) != null)
                {
                    strValue += StrDate + "\r\n";
                }
                return strValue;
            }
            catch (Exception ex)
            {
                Console.WriteLine("getUrl = " + strUrl + "  Exception" + ex);
                //LogHelper.CreateErrorLogTxt("PostJson", ex.Source.ToString(), ex.Message);
            }
            finally
            {
                if (myStreamReader != null)
                {
                    myStreamReader.Close();
                }
                if (myResponseStream != null)
                {
                    myResponseStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return "{\"soundCode\":\"1419\",\"code\":\"1\",\"message\":\"網絡連接異常\"}";
        }

    }

 

發佈了57 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章