C# 使用HttpWebRequest获取功能时,TCP连接堆叠到65k-internet连接丢失
我不明白是什么导致了这个错误 我正在使用下面的函数和大约1000个并发连接 每个连接使用不同的webproxy 工作15分钟后,已建立的TCP连接计数开始堆叠,internet连接丢失 当我不使用任何webproxy时,我不会遇到任何错误 我正在使用下面的函数检索活动TCP连接数C# 使用HttpWebRequest获取功能时,TCP连接堆叠到65k-internet连接丢失,c#,tcp,httpwebrequest,webrequest,webproxy,C#,Tcp,Httpwebrequest,Webrequest,Webproxy,我不明白是什么导致了这个错误 我正在使用下面的函数和大约1000个并发连接 每个连接使用不同的webproxy 工作15分钟后,已建立的TCP连接计数开始堆叠,internet连接丢失 当我不使用任何webproxy时,我不会遇到任何错误 我正在使用下面的函数检索活动TCP连接数 var properties = IPGlobalProperties.GetIPGlobalProperties(); 我没有发现我的功能有任何漏洞 所以我需要你的帮助来解决这个烦人的问题 c#.net 4.6.2
var properties = IPGlobalProperties.GetIPGlobalProperties();
我没有发现我的功能有任何漏洞
所以我需要你的帮助来解决这个烦人的问题
c#.net 4.6.2
这里是发生此问题时活动TCP连接的状态
我这里的主要建议是不要使用
WebRequest
,而是使用更新的HttpClient
。如果没有其他功能,它支持IDisposable
(a)使用同步API永远无法获得高吞吐量。(b) 使用异步API时,请注意DNS查找阶段是同步的。这可以通过第三方DNS库(也许)(c)来缓解(),根据我爬网的经验,最有可能的故障点(一旦你完全确定你的代码正常工作)是在路由器上。家庭路由器往往不足以进行大容量爬网,当它们的内存耗尽时,连接就会消失。@spender它可以完美地工作15-20分钟。在那之后,出现了一些奇怪的错误。我确信会发生一些错误,因为已建立的连接数开始累积。我在这里不知道:(我的家庭路由器很好。带宽从未达到最大值。我将尝试提供IP主机。可能是关于DNS的。@DavidG我已经检查过了。它不支持代理:(它绝对支持代理)。
public static cs_HttpFetchResults func_fetch_Page(
string srUrl, int irTimeOut = 60,
string srRequestUserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0",
string srProxy = null, int irCustomEncoding = 0, bool blAutoDecode = true, bool blKeepAlive = true)
{
cs_HttpFetchResults mycs_HttpFetchResults = new cs_HttpFetchResults();
mycs_HttpFetchResults.srFetchingFinalURL = srUrl;
HttpWebRequest request = null;
WebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(srUrl);
request.CookieContainer = new System.Net.CookieContainer();
if (srProxy != null)
{
string srProxyHost = srProxy.Split(':')[0];
int irProxyPort = Int32.Parse(srProxy.Split(':')[1]);
WebProxy my_awesomeproxy = new WebProxy(srProxyHost, irProxyPort);
my_awesomeproxy.Credentials = new NetworkCredential();
request.Proxy = my_awesomeproxy;
}
else
{
request.Proxy = null;
}
request.ContinueTimeout = irTimeOut * 1000;
request.ReadWriteTimeout = irTimeOut * 1000;
request.Timeout = irTimeOut * 1000;
request.UserAgent = srRequestUserAgent;
request.KeepAlive = blKeepAlive;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-gb,en;q=0.5");
myWebHeaderCollection.Add("Accept-Encoding", "gzip, deflate");
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
using (response = request.GetResponse())
{
using (Stream strumien = response.GetResponseStream())
{
Encoding myEncoding = Encoding.UTF8;
string srContentType = "";
if (response.ContentType != null)
{
srContentType = response.ContentType;
if (srContentType.Contains(";"))
{
srContentType = srContentType.Split(';')[1];
}
srContentType = srContentType.Replace("charset=", "");
srContentType = func_Process_Html_Input(srContentType);
}
try
{
myEncoding = Encoding.GetEncoding(srContentType);
}
catch
{
myEncoding = irCustomEncoding == 0 ? Encoding.UTF8 : Encoding.GetEncoding(irCustomEncoding);
}
using (StreamReader sr = new StreamReader(strumien, myEncoding))
{
mycs_HttpFetchResults.srFetchBody = sr.ReadToEnd();
if (blAutoDecode == true)
{
mycs_HttpFetchResults.srFetchBody = HttpUtility.HtmlDecode(mycs_HttpFetchResults.srFetchBody);
}
mycs_HttpFetchResults.srFetchingFinalURL = Return_Absolute_Url(response.ResponseUri.AbsoluteUri.ToString(), response.ResponseUri.AbsoluteUri.ToString());
mycs_HttpFetchResults.blResultSuccess = true;
}
}
}
if (request != null)
request.Abort();
request = null;
}
catch (Exception E)
{
if (E.Message.ToString().Contains("(404)"))
mycs_HttpFetchResults.bl404 = true;
csLogger.logCrawlingErrors("crawling failed url: " + srUrl, E);
}
finally
{
if (request != null)
request.Abort();
request = null;
if (response != null)
response.Close();
response = null;
}
return mycs_HttpFetchResults;
}