The request was aborted: Could not create SSL/TLS secure channel

后端 未结 30 1749
遇见更好的自我
遇见更好的自我 2020-11-22 01:21

We are unable to connect to an HTTPS server using WebRequest because of this error message:

The request was aborted: Could not create SSL/TLS secur

相关标签:
30条回答
  • 2020-11-22 01:49

    If you are running your code from Visual Studio, try running Visual Studio as administrator. Fixed the issue for me.

    0 讨论(0)
  • 2020-11-22 01:50

    In case that the client is a windows machine, a possible reason could be that the tls or ssl protocol required by the service is not activated.

    This can be set in:

    Control Panel -> Network and Internet -> Internet Options -> Advanced

    Scroll settings down to "Security" and choose between

    • Use SSL 2.0
    • Use SSL 3.0
    • Use TLS 1.0
    • Use TLS 1.1
    • Use TLS 1.2

    0 讨论(0)
  • 2020-11-22 01:51

    After many long hours with this same issue I found that the ASP.NET account the client service was running under didn't have access to the certificate. I fixed it by going into the IIS Application Pool that the web app runs under, going into Advanced Settings, and changing the Identity to the LocalSystem account from NetworkService.

    A better solution is to get the certificate working with the default NetworkService account but this works for quick functional testing.

    0 讨论(0)
  • 2020-11-22 01:52

    As you can tell there are plenty of reasons this might happen. Thought I would add the cause I encountered ...

    If you set the value of WebRequest.Timeout to 0, this is the exception that is thrown. Below is the code I had... (Except instead of a hard-coded 0 for the timeout value, I had a parameter which was inadvertently set to 0).

    WebRequest webRequest = WebRequest.Create(@"https://myservice/path");
    webRequest.ContentType = "text/html";
    webRequest.Method = "POST";
    string body = "...";
    byte[] bytes = Encoding.ASCII.GetBytes(body);
    webRequest.ContentLength = bytes.Length;
    var os = webRequest.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
    os.Close();
    webRequest.Timeout = 0; //setting the timeout to 0 causes the request to fail
    WebResponse webResponse = webRequest.GetResponse(); //Exception thrown here ...
    
    0 讨论(0)
  • 2020-11-22 01:52

    Try this:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
    0 讨论(0)
  • 2020-11-22 01:55

    Make sure the ServicePointManager settings are made before the HttpWebRequest is created, else it will not work.

    Works:

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
           | SecurityProtocolType.Tls11
           | SecurityProtocolType.Tls12
           | SecurityProtocolType.Ssl3;
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com/api/")
    

    Fails:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com/api/")
    
    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
           | SecurityProtocolType.Tls11
           | SecurityProtocolType.Tls12
           | SecurityProtocolType.Ssl3;
    
    0 讨论(0)
提交回复
热议问题