我在生產服務器上某時(上午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;
請問您可以添加您的代碼 –
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; –