3

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.

Sean
  • 822
  • 7
  • 26
  • have you tried req.Proxy = WebProxy.GetDefaultProxy(); – Amit Pore Jun 02 '16 at 10:35
  • Have just tried using the GetDefaultProxy (which has been deprecated?) and it made no difference. – Sean Jun 02 '16 at 10:45
  • ooh sorry about that ,guess rather than contentype you can use this req as HttpWebRequest.Accept = "text/xml"; – Amit Pore Jun 02 '16 at 10:56
  • also you can refer to Jon Skeet's answer to use Wireshark http://stackoverflow.com/questions/4207428/webservice-error-the-underlying-connection-was-closed-an-unexpected-error-oc?rq=1 – Amit Pore Jun 02 '16 at 11:16
  • Swapped the ContentType for Accept ... no change there. – Sean Jun 02 '16 at 12:25
  • Have installed WireShark and filtered on the destination IP address; which is showing some 'stuff', but being unfamiliar with WireShark, I am not really sure what I am looking at; I see: SSL Protocol : Length 194 : Client Hello -> 443 [ACK] Seq=141 Ack=1 Win=64240 Len=0 -> 443 [FIN, ACK] Seq=141 Ack=2 Win=64240 Len=0 Can provide more details from WireShark if I can get some guidance as to what I need to look for. – Sean Jun 02 '16 at 12:28

2 Answers2

6

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.

Sean
  • 822
  • 7
  • 26
0
       //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();
Amit Pore
  • 127
  • 2
  • 13
  • I'm not quite sure what is significantly different with this code, compared to the code that I have been using; but have tried this ... same error and I really suspect that there's something going on which is aside the code, as the same requests to google.com and a simple text file (uploaded to my own server) both worked as I would have expected. Investigations continue – Sean Jun 02 '16 at 14:43
  • I understand , just a try . The change is in contentType ""application/x-www-form-urlencoded"" have a look at this post http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data – Amit Pore Jun 02 '16 at 14:50