2016-05-31 110 views
0

我在生產服務器上某時(上午9點到下午2點)收到錯誤錯誤消息,但測試服務器上沒有錯誤。 實際上,我使用http請求從不同網站中提取數據。 它每天的每個小時運行從網站獲取數據出現錯誤:現有連接被遠程主機強制關閉c#

錯誤:

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 

CODE:

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

請問您可以添加您的代碼 –

+0

HttpWebRequest webRequest = WebRequest.Create(URL)爲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;返回responseData; –

回答

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; 
} 
+0

謝謝,我試圖在我的邏輯上添加這些行,看看生產server.I通知你,如果它運行;) –

+0

仍然收到同樣的錯誤:(@Pranav Patel –

+0

@WebashlarDevelopers,在測試服務器上工作? –

相關問題