The underlying connection was closed: An unexpected error occurred

I have a very simple service which calls to a URL and captures a status that is written out by that service;

// Service call used to determine availability
System.Net.WebClient client = new System.Net.WebClient();

// I need this one (sorry, cannot disclose the actual URL)
Console.WriteLine(client.DownloadString(myServiceURL + ";ping"));

// I added this for test purposes
Console.WriteLine(client.DownloadString("https://www.google.com"));

The "DownloadString" for myServiceURL line throws the error "The underlying connection was closed: An unexpected error occurred" and there's nothing showing in Fiddler for this line, whereas the "DownloadString" for google.com works and I see the console output for that.

Following other suggestions for the error, I have tried combinations of setting UseDefaultCredentials, Encoding options, adding appropriate headers to the request, none of which make any difference.

client.UseDefaultCredentials = true;
client.Encoding = Encoding.UTF8;

When I navigate to the myServiceURL in a browser, it works and shows "OK", as expected.

Another method from the same service has been coded as follows:

// Request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(myServiceURL + ";login");

// Set the request configuration options
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = bytes.Length;
req.Timeout = -1;

// Call for the request stream
using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}

// ....snip

// This line fails with the same error as before
WebResponse resp = req.GetResponse()

This is all being run on a Windows 7 (64-bit) PC using .NET Framework 4.0; the service at myServiceURL is a 3rd-party service for which I have no control over.


Have finally got to the bottom of this and whilst the answer may not apply to everyone with the same problem; I would suggest that the clue was in the fact that we could pull information from some HTTPS site, but not all and tracing events through a combination of Fiddler, WireShark and our Firewall.

Opening the sites in Google Chrome and clicking the padlock for 'https' in the URL address for the site, to view the 'Security Overview' we see that for most of the sites we tried that there are entries listed for 'Valid Certificate' and for 'Secure Resources', but this one site also had an entry for 'Secure TLS Connection' and WireShark confirmed that the handshake (from Chrome) was using TLS v1.2

TLS v1.2 appears to only be supported with .NET Framework 4.5 (or above), which therefore needs Visual Studio 2012 (or above)

We are currently running .NET Framework 4.0 with Visual Studio 2010

Downloaded Visual Studio 2015 Community Edition to test the "same" code within a project using .NET Framework 4.5.2 and it worked straight away.


       //assuming this is set
        byte[] Data;
        string url = string.Format("{0};{1}" ,myServiceURL, "login");
        // Request
        HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);

        wreq.Method = "POST";
        wreq.Proxy = WebProxy.GetDefaultProxy();

        (wreq as HttpWebRequest).Accept = "text/xml";


        if (Data != null && Data.Length > 0)
        {
            wreq.ContentType = "application/x-www-form-urlencoded";
            System.IO.Stream request = wreq.GetRequestStream();
            request.Write(Data, 0, Data.Length);
            request.Close();
        }
        WebResponse wrsp = wreq.GetResponse();
链接地址: http://www.djcxy.com/p/22160.html

上一篇: 内容中的特殊字符

下一篇: 底层连接已关闭:发生意外错误