2

Normally, this code works completely fine, but I have a single instance on a customers PC which is throwing this exception. We can't replicate at all. As in the subject, the exception I am getting is "The underlying connection was closed: An unexpected error occurred on a receive." I have researched the hell out of this but all I could find was setting KeepAlive to false, which I have already done. This is normally an SSL connection, but I have had the customer attempt the same connection over standard http and we got the same results.

It is a super fast web request usually, with the userinfo class having about 10 string elements, and the response in a string of about 100 characters after a tiny MySQL query. So I don't think this is a timeout thing.

Any ideas?

public static string HitServer(string url)
        {

            if (UserInfo.Instance == null) return "";
            //Request update XML path
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.KeepAlive = false;
            req.Method = "POST";
            req.ContentType = "xml/text";
            using (Stream reqStream = req.GetRequestStream())
            {
                XmlSerializer s = new XmlSerializer(typeof(UserInfo));
                s.Serialize(reqStream, UserInfo.Instance);
            }

            WebResponse response = req.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream);
            return sr.ReadToEnd();
        }

2 Answers 2

1

After trying all kind of fixes like HTTP protocol changes, Expect100contine, keepalive and a handful of other request parameters, I had to switch to non-SSL and just encrypt the password over this connection.

Not the most ideal fix, but it had to be done. It seems that something on certain PC's would block SSL connections from our application. This was tested by using the browser view built into our software to attempt to load an image on a secure server - this failed.

0

When I get the "The underlying connection was closed: An unexpected error occurred on a receive.", it is usually caused by serialization and/or deserialization errors. I would guess that the problem occurs when deserializing the response on the client, if no errors are logged on the server.

How about registering a module to catch all errors on the client, and check if you get any errors?

3
  • I don't actually deserialise anything on the client, it just comes back as a string (above code is client). On the server end, it is just some simple php simplexml_load_string($text), then using $xml->Element to extract the required fields
    – James R
    Aug 3, 2011 at 7:06
  • What happens if you call the URL from the client having the problem through a browser? Does it work as expected? How about if you turn off proxies in Internet Explorer? Does the request work? Does your .net client code work? (The "default proxy" setting in .NET implies some quite ugly logic to figure out which proxy is the default) Aug 3, 2011 at 7:09
  • I've had the customer hit the URL in a browser and it seems to work. He also isn't using a proxy - I thought that may have been the problem earlier. Seems crazy that this is the only case of this error we've had.
    – James R
    Aug 4, 2011 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.