English 中文(简体)
IOException when making HttpWebRequest to local ASHX file
原标题:

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

问题回答

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.





相关问题
ASP.NET WebService deny remote access

I ve created an ASP.NET WebService that is to be consumed using ASP.NET Ajax. The WebService is located on the same box and same web application that it is to be used by, so I do not want to allow ...

IOException when making HttpWebRequest to local ASHX file

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:...

Localhost issue with Net.Msmq endpoints on Windows 7

I ve just moved my development to a Win 7 64-bit machine and am having some wcf endpoint issues. As far as I can see the net.msmq endpoints that point to localhost do not work. The messages do get ...

Allowing "Cross-Site" calls between local ports

I m working on a Google Web Toolkit driven site that communicates via AJAX to a WCF server. Once deployed the GWT code will run in the same domain as the WCF service but when developing/debugging ...

Rmi connection refused with localhost

I have a problem using java rmi: When I m trying to run my server, I get a connectException (see below). Exception happens when executing the rebind method: Runtime.getRuntime().exec("rmiregistry ...

Updating your blog from a localhost version

Okay, I trust this is not a ServerFault question, though it has something to do with server settings, the main issue might concern some programming. I use Wordpress as my blogging platform, only ...

send email from localhost

I m try learn about email in rails. I m developing something on localhost. Is it possible to send an email from localhost to say a normal mail account like gmail? Do I have a install a mail server? I ...

Visual Studio Localhost Redirect Problem Asp.net

I have a test site running on Visual Studio 2008. When I try to debug and it opens up the browser through localhost, I can this error"Firefox has detected that the server is redirecting the request ...

热门标签