0

Following code worked file until server IP changed. I use domain in URL.

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://example.com/oauth/token?grant_type=password&username=" + username + "&password=" + password);
            request.Method = "POST";
            request.Headers.Add("Authorization", "Basic ......");

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string result = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();

Error:

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

But postman does it.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Orgil
  • 187
  • 3
  • 13
  • 1
    Do you have Tls settings on the ServicePointManager? Make sure they include the protocols needed by the server. If it is a new server expect Tls13 to be required, see also: https://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel or you have a cipher mismatch: https://stackoverflow.com/questions/60766145/system-net-webexception-the-request-was-aborted-could-not-create-ssl-tls-sec – rene Oct 26 '20 at 06:45
  • 1
    Not only the IP changed, the server's TLS settings changed. – CodeCaster Oct 26 '20 at 06:55
  • tried ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;still got no luck – Orgil Oct 26 '20 at 07:07
  • tried add ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; on top but got error "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel" – Orgil Oct 26 '20 at 07:09
  • 1
    Don't you have Tls13? What framework version are you on? What is your OS? – rene Oct 26 '20 at 07:19
  • 1
    Are you sure all certificates that come back from that server are trusted: https://stackoverflow.com/questions/55620230/could-not-establish-trust-relationship-for-the-ssl-tls-secure-channel-with-auth ? – rene Oct 26 '20 at 07:21
  • When I add ServicePointManager.SecurityProtocol = (SecurityProtocolType)12288; on top, error changed to "An unexpected error occurred on a receive". Is that mean Tls is Tls13? – Orgil Oct 26 '20 at 07:34
  • My computer's framework is 4.5, windows 10. Don't know server information – Orgil Oct 26 '20 at 08:22

0 Answers0