views:

305

answers:

2

Greetings, all. Here is my situation. I am attempting to make an HttpWebRequest to a local handler file and I keep getting the following exception:

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Now, I'm using a local handler file because I am writing some integration code for a third party that the site will be using. Until I have a test environment available for me to make requests to, I'm basically mocking the process with a local handler file. Here is the relevant code. Thanks.

WebRequest code (subRequest variable is object passed to the method executing this code):

XmlSerializer serializer;
XmlDocument xmlDoc = null;

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml";
webRequest.KeepAlive = true;
webRequest.Accept = "*/*";

serializer = new XmlSerializer(subRequest.GetType());
XmlWriter writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8);
serializer.Serialize(writer, subRequest);
writer.Close();

xmlDoc = new XmlDocument();
xmlDoc.Load(XmlReader.Create(webRequest.GetResponse().GetResponseStream()));

The "requestUrl" is defined as "http://localhost:2718/Handlers/MyHandler.ashx". I can hit the handler file just fine and have stepped through the code. All it does is assemble an XML response as a string and writes it out to the Response object:

context.Response.ContentType = "text/xml";
string newSubscriptionId = Utils.GetUniqueKey();

StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
// Assemble XML string here

context.Response.Write(sb.ToString());

As far as I can tell, this is all working just fine. But when my code hits the last line of the WebRequest chunk:

xmlDoc.Load(XmlReader.Create(webRequest.GetResponse().GetResponseStream()));

Is when the exception is thrown. Any ideas? Thanks in advance.

James

A: 

It is difficult to say what the root cause is, without more info. You could try getting a system.net tracelog http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html. Also, a wireshark trace would help figure out why the remote server is closing the connection.

feroze
A: 

First, if you don't intend to reuse the connection or there's not going to be another request to the same schema/server/port, I would set KeepAlive to false.

The problem is that XmlDocument.Load() does not read the entire stream before the server closes the connection or that it keeps reading beyond the end and when the server keep-alive timeout is over, the connection is closed by the server. Also, you never close the response stream. To verify that this theory is correct, do something like:

// Optional -> webRequest.KeepAlive = false;
string xml = null;
using (StreamReader reader = new StreamReader (webRequest.GetResponse().GetResponseStream())) {
    xml = reader.ReadToEnd ();
}
xmlDoc.LoadXml (xml);

and see if that fixes your problem.

Gonzalo