c#调试错误:已存在的连接被远程主机强制关闭
本文关键字:主机 程主机 错误 调试 存在 连接 | 更新日期: 2023-09-27 18:07:32
我正在尝试使用代理服务器抓取网站。这是我的代码
try{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(proxy+link);
req.Timeout = 60000;//timeout 2 minutes
req.UserAgent = AGENTLIST[agentCount];
req.ServicePoint.ConnectionLimit = 1;
//response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string html = reader.ReadToEnd();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
body = doc.DocumentNode.SelectSingleNode("//body");
}catch(Exception e)
{ Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.ReadLine();
}
我得到这个错误
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at ASP.osproxy_getpage_aspx.Page_Load(Object sender, EventArgs e) in c:'SharedCrawl'Dropbox'sharedcrawl'osproxy'GetPage.aspx:line 30
如果我这样做
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(link); //no proxy
我没有得到错误。
谁能解释一下这个错误是什么意思?
谢谢R
这不是你在webrequest上使用代理的方式。你应该使用HttpWebRequest。代理财产。
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(link);
req.Proxy = new WebProxy(proxyAddress, proxyPort);