|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
SocketException - WebResponse Issuedebug. I'm going to document it here in case anyone else has a similar problem. I have a simple app that performs on-demand data transformations between two systems. During this process, it has to download N number of documents from a specific website. I accomplished this using the WebRequest and WebResponse classes. My code looked something like this: foreach (Document doc in documentList) { WebRequest request; WebResponse response; Stream responseStream = null; try { request = WebRequest.Create(doc.URI); response = request.GetResponse(); responseStream = response.GetResponseStream(); ... code to read stream & process document ... } finally { if (responseStream != null) { responseStream.Close(); } } } I was sporadically encountering the following error: SocketException {"An existing connection was forcibly closed by the remote host"} " at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\r\n at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)" After some troubleshooting, I determined that the exception was being thrown when the app tried to read from a new response stream after reading more than 20 MB from other response streams. For example, it needs to process four documents that are each 7 MB. It would process the first three fine and then fail on the first call to responseStream.Read for the fourth document. The issue may be related to this KB article: http://support.microsoft.com/default.aspx?scid=kb;en-us;826756. I have no control over the webserver. I finally resolved this issue by explicitly closing the WebResponse. For some reason, I was under the impression that closing the response stream was sufficient. After I added the following code to the finally block, my app works without problem. I realize that calling response.Close should close the responseStream as well, but when I'm having issues with code, I like to be explicit. finally { if (responseStream != null) { responseStream.Close(); } if (response != null) { response.Close(); } } I hope this helps somebody else save a few hours. Search Keywords: ConnectStream, HttpWebRequest, HttpWebResponse, System.IO.IOException: Unable to read data from the transport connection: |
|||||||||||||||||||||||