五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > HttpWebRequest 基礎(chǔ)連接已經(jīng)關(guān)閉: 接收時發(fā)生錯誤的解決方法

HttpWebRequest 基礎(chǔ)連接已經(jīng)關(guān)閉: 接收時發(fā)生錯誤的解決方法

文章來源:365jz.com     點擊數(shù):9685    更新時間:2020-06-27 22:55   參與評論

HttpWebRequest 基礎(chǔ)連接已經(jīng)關(guān)閉: 接收時發(fā)生錯誤 

HttpWebRequest 基礎(chǔ)連接已經(jīng)關(guān)閉: 發(fā)送時發(fā)生錯誤 

HttpWebRequest 基礎(chǔ)連接已關(guān)閉 連接意外關(guān)閉

報的錯誤為:1. "基礎(chǔ)連接已經(jīng)關(guān)閉: 發(fā)送時發(fā)生錯誤";

之前的寫法:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

能解決問題的寫法:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

分析:因為請求的url是基于https的,所以Post請求時必須添加ServicePointManager.SecurityProtocol。但選擇哪個協(xié)議呢?一開始看到項目里面都是基于ssl的,索性也用了ssl,但是沒有效果,最后干脆直接把所以的協(xié)議枚舉用或的形式全都寫出來,成功了。

SSL驗證一定要寫在WebRequest.Create(url)前面


針對使用httpWebRequest優(yōu)化的幾種解決方案:


      1、在GetResponse() 前加上這句ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;  這句代碼是讓你的程序適應(yīng)https請求協(xié)議。


      2、httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);  在行代碼前添加System.GC.Collect();   用于程序垃圾資源回收


      3、如果Http的請求,是設(shè)置了KeepAlive=true的話,那么對應(yīng)的http的connection會和服務(wù)器保持連接的。所以如果上述辦法都不能解決超時的問題,可以嘗試將keepAlive設(shè)置為false試試,看看能否解決。


      4、httpWebRequest.ServicePoint.ConnectionLimit = maxTry;  默認(rèn)ConnectionLimit是2個連接數(shù),所以可以通過修改連接數(shù)嘗試解決該問題??梢愿牡?00-300,但是不要改太大,容易對程序照成壓力。


      5、另外你初始化的都要在用完之后,進(jìn)行關(guān)閉和回收。(HttpWebRequest  HttpWebResponse)     雖然每種開發(fā)語言都有自己的回收機(jī)制,但是要記著一點再好的人,也有累的時候,累了就不給你干活了,所以平時對它們好點。


      如果上面的方法都無法解決你的問題,可以嘗試一下我最后的解決方案。


      最后我發(fā)現(xiàn)只有在一臺服務(wù)器上面出問題,后來經(jīng)過查找,發(fā)現(xiàn)是這臺服務(wù)器被配置了代理服務(wù)器,通過代理服務(wù)器的方式進(jìn)行外網(wǎng)的訪問。所以找到原因就好辦了,只要在創(chuàng)建HttpWebRequest  對象之前,在創(chuàng)建一個代理服務(wù)器的對象,并且把服務(wù)器的代理地址和端口實例化到代理服務(wù)器對象。


public  string GetHtml(string url, byte[] postData, bool isPost, CookieContainer cookieContainer,string refurl)
        {
            ServicePointManager.Expect100Continue = false;
            Thread.Sleep(NetworkDelay);//等待
            currentTry++;
            HttpWebRequest httpWebRequest = null;
           
            HttpWebResponse httpWebResponse = null;
            try
            {
               // byte[] byteRequest = Encoding.Default.GetBytes(postData);
                byte[] byteRequest = postData;
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                if (Proxy != null)
                    httpWebRequest.Proxy = Proxy;      //代理服務(wù)器設(shè)置
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = refurl;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;
                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                currentTry = 0;
                httpWebRequest.Abort();
                httpWebResponse.Close();
                foreach (Cookie cookie in httpWebResponse.Cookies)
                {
                    cookieContainer.Add(cookie);
                }
                return html;
            }
            catch (Exception)
            {
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return "";
            }
        }


如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答!! 點擊進(jìn)入論壇

發(fā)表評論 (9685人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
2022年01月24日 11:24apollo

不錯 推薦

------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號