1.原因:
(1)KeepAlive默認為true,與internet保持持續連接 ,服務器關閉了連接,使用HttpWebResponse.GetResponse()出錯
(2)HttpWebRequest 超過了最大連接數
(3)網絡響應慢而導致超時
2.解決:
(1) req.KeepAlive = false;
(2)設置超時時間 req.Timeout = 15000;
(3)連續使用HttpWebRequest請求時,要使用sleep(100),放大http請求時間間隔
(4) 突破該程序的http的最大連接數
ServicePoint 對象允許的最大並發連接數。默認值為 2。
1,在代碼中修改:
system.net.ServicePointManager.DefaultConnectionLimit=100//把最大連接數改為100
2,在配置文件(app.config)中修改: <configuration> <system.net> <connectionManagement> <!--表示把對任何域名的請求最大http連接數都設置為100--> <add address = "*" maxconnection = "100" /> </connectionManagement> </system.net> </configuration>
(5)保證每次用完都要關閉
if (res != null) { res.Close(); res = null; } if (req != null) { req.Abort(); req = null; }