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

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.
Peter ChikovAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team LeadCommented:
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
hilltopCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.