0

Hi I am trying to do a HttpRequest to a website and when I call GetRequestStream() I keep getting this Error.

My code:

        byte[] data = Encoding.ASCII.GetBytes(myJson);
        // Create a request for the URL. 
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            ServicePointManager.Expect100Continue = true;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
          "https://host-address.com:443");
        request.ServerCertificateValidationCallback += (sender, certificate2, chain, sslPolicyErrors) => {
            return true;
        };

        X509Certificate2Collection certificates = new X509Certificate2Collection();
       certificates.Import("C:\\Certs\\MyCertificate.pfx", "myCertPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

        request.ClientCertificates = certificates;

        request.Method = "POST";
        request.ContentType = "text/json";
        request.ContentLength = data.Length;
        request.UserAgent = "My User Agent";
        request.ProtocolVersion = HttpVersion.Version11;
        request.KeepAlive = true;

        Console.WriteLine("Requesting connection....");
        //The exception is thrown here
        using (Stream stream = request.GetRequestStream()) //Here's where the exception happens
        {

            stream.Write(data, 0, data.Length);
        }

I already tried this Could not create SSL/TLS secure channel, despite setting ServerCertificateValidationCallback but not seems to work, also set the delegate ServerCertificateValidationCallback to always return true but it's not even reaching that part.

This might be a cypher problem? Due that I can get a response from the page when I browse it on chrome but not with IE.

Jozkee
  • 77
  • 2
  • 13

1 Answers1

0

I would recommend following:

  1. Download server SSL certificate locally and check its chain validity to local trusted root CA.
  2. Check server SSL certificate info on https://www.ssllabs.com/ssltest/analyze.html
  3. Make sure user account under which application operates trusts that root CA (may me you need global machine storage, not local storage).
  4. Try simple GET request from application.

Hope this could give you a clue what's wrong.

Adam
  • 1,796
  • 18
  • 19