我在某个时间(上午9点到下午2点)在生产服务器上出现错误,但在测试服务器上没有。 实际上,我使用http请求从不同的网站上废弃了数据。 它每小时运行一次
错误:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
---> 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
代码:
HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
response.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
webRequest.KeepAlive = false;
return responseData;
答案 0 :(得分:1)
尝试在代码中添加以下行
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAll);
并且函数将被定义为
public bool AcceptAll(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}