HttpWebRequest with Authorization throws Connection was forcibly closed(conversion from Java)

Peter Chikov
Peter Chikov used Ask the Experts™
on
This code in Java works perfectly:

    URL url = new URL(PREREQ_URL);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("Authorization", authorization);
    System.out.println(authorization);

    InputStream is = (InputStream) con.getInputStream();
    BufferedReader in
            = new BufferedReader(new InputStreamReader(is));

    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    in.close();

Open in new window


I am trying to convert it into C# and I am getting
System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a send.'

This is what I have:

    Uri prereqUri = new Uri(PREREQ_URL);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(prereqUri);
    //request.Headers["Authorization"] = authorization;
    request.Headers.Add("Authorization", authorization);
    //request.PreAuthenticate = true;
    request.Method = "POST";
    //var postData = "Authorization=" + authorization;
    //var data = Encoding.ASCII.GetBytes(postData);
    //request.ContentLength = data.Length;

    //using (var stream = request.GetRequestStream())
    //{
    //   stream.Write(data, 0, data.Length);
    //}

    var response = (HttpWebResponse)request.GetResponse();

Open in new window


I have also tried the commented out code with the same problem. Any ideas, please.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
The underlying connection was closed: An unexpected error occurred on a send
I was facing this issue couple of years ago and found out it was due to TLS version issues.

i don't have that piece of source code with me anymore, but you can refer to this discussion:

“The underlying connection was closed: An unexpected error occurred on a send.” With SSL Certificate
https://stackoverflow.com/questions/22627977/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send
If you want to ignore SSL errors, give this a shot. You can set the RemoteCertificateValidationCallback to true anytime you make a request. An example is below.


using System.Security.Cryptography.X509Certificates;

Open in new window


  private static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
        {
            return true;
        }

Open in new window


ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
    ValidateRemoteCertificate);
    Uri prereqUri = new Uri(PREREQ_URL);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(prereqUri);
      request.Headers.Add("Authorization", authorization);
    
      request.Method = "POST";
      var response = (HttpWebResponse)request.GetResponse();

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial