0

I'm trying to get a json string via WebHttpRequest using the code below (it tries to call a file that will return the json for me which is in the application). It worked on my machine but when I deployed to the user server, I got the error

"The underlying connection was closed: An unexpected error occurred on a send.".

I got the same error when I try to run the powershell invoke-webrequest for the URL, but weirdly it works fine when I use Postman (get or post) for the URL, inside or outside the user server.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/json";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
String jsonResponse = reader.ReadToEnd();

I also tried to use another method (WebClient) to get the URL, but it didn't work as well.

String jsonResponse = null;
using (var webClient = new WebClient())
{
    jsonResponse = webClient.DownloadString(url);
}

The full error I get on the exception:

The remote server returned an error: (401) Unauthorized. System at System.Net.HttpWebRequest.GetResponse()

Does anyone have any idea?

Thank you.

erhj89
  • 3
  • 5
  • Update the question with the status code.Use try catch put the innerexception here – Debashish Saha Jun 06 '18 at 19:13
  • If I recall, that error message propogates for multiple reasons, but the most common in my experience is that the endpoint could not be reached, or that the server is actively refusing the connection. Have you checked server logs for the endpoint you're hitting (assume you have access)? – cwharris Jun 06 '18 at 19:30
  • Question updated with the error I get on the try catch. – erhj89 Jun 06 '18 at 20:12
  • If your request requires a Secure Http connection (you have "Https:" in the Uri), you need to enable a Security protocol supported by the Server (as shown in one of the answers). You also need to validate the Server certificate. See [ServicePointManager.ServerCertificateValidationCallback](https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.servercertificatevalidationcallback(v=vs.110).aspx) – Jimi Jun 06 '18 at 20:28
  • I added the following code `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };` right above the `HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);` but I didnt have luck, it didnt work. – erhj89 Jun 07 '18 at 17:07
  • Can you share that URI, so it can be tested? – Jimi Jun 07 '18 at 21:59
  • Sorry, I was testing the code on one of the user environments but that one was requiring a password and that's why it wasnt working. But I tested in the correct one and the ServicePointManager code worked. Thank you for the help – erhj89 Jun 08 '18 at 13:28

1 Answers1

1

I think that this might be your case "The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate

It seems that you would need to set ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

BTW you have in your code:

request.UseDefaultCredentials = true; request.PreAuthenticate = true; request.Credentials = CredentialCache.DefaultCredentials;

So what are credentials? Is that network credentials like AD credentials? Is there any authorization to get access to that server?

jkosmala
  • 94
  • 1
  • 2
  • 6
  • Hi, it does not require any credential to access that url via browser or postman, it simply give back the json fine. But via HttpWebRequest it doesn't work. I will try the SSL Certificate, thanks. – erhj89 Jun 06 '18 at 20:01
  • @erhj89 `it does not require any credential to access` the point here is the *certificate* not credentials. I think this answer may be a good point to investigate. – Eser Jun 06 '18 at 20:20
  • did you tried different version of TLS or using SSL3.0? Did you tried to remove credentials piece? If it's public site with json response - could you share URL? – jkosmala Jun 06 '18 at 20:43
  • I tried many combinations,for example `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3` or `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;` or `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;` and also all that without the credentials piece, but nothing worked. Unfortuantely the user doesnt allow me to publish the url, sorry for that. – erhj89 Jun 07 '18 at 15:40
  • One thing to add, I was putting `ServicePointManager.SecurityProtocol` right before my request. Is that correct? – erhj89 Jun 07 '18 at 16:31
  • It should work as you did. You may try taking fiddler as a proxy, and compare in details requests sent by your application and by browser. – jkosmala Jun 07 '18 at 19:39
  • It's resolved. I was testing the code on one of the user environments but that one was requiring a password and that's why it wasnt working. But I tested in the correct one and the ServicePointManager code worked. Thank you all for the help – erhj89 Jun 08 '18 at 13:27