Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am invoking a request to Core. I am sending json request. I am using asp.net with c#. Proxy is disabled in the browser. In Postman I am getting response though.
Exception happening while trying to get the request stream:
The Request was aborted. Could not create SSL/TLS secure channel.

What I have tried:

My code is as below:
string RequestURL = txtRequestURL.Text;
              string jsonRequest = txtRequest.Text;

              txtLog.Text += "Transact_Posting_Core()-Creating/Assigning HTTP Web Request Process Started with Request URL:" + RequestURL + ",Request:" + jsonRequest + Environment.NewLine;

                  //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                    | SecurityProtocolType.Tls11
                    | SecurityProtocolType.Tls12
                    | SecurityProtocolType.Ssl3;


              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RequestURL);
              System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
              //request.Host = "";
              request.Method = "POST";
              request.Accept = @"application/json";
              request.ContentType = @"application/json";
              Byte[] byteArray = encoding.GetBytes(jsonRequest);
              request.ContentLength = byteArray.Length;
              //request.Proxy = null;
             //string isProxyReqd = ConfigurationManager.AppSettings["isProxyReqd"] != null ? ConfigurationManager.AppSettings["isProxyReqd"].ToString() : string.Empty;
             // if (isProxyReqd == "1")
             // {
             //     request.Proxy = new WebProxy();
             // }

              request.ProtocolVersion = HttpVersion.Version10;

              string IsRequestHeaderFlag = ConfigurationManager.AppSettings["IsRequestHeaderFlag"] != null ? ConfigurationManager.AppSettings["IsRequestHeaderFlag"].ToString() : string.Empty;
              if (IsRequestHeaderFlag == "1")
              {
                  request.Headers.Add("X-API-KEY", txtXAPIKey.Text);
              }
              txtLog.Text += "Transact_Posting_Core()-Started Transact Posting Request Stream Process" + Environment.NewLine;
              using (Stream stm = request.GetRequestStream())

              {
                  stm.Write(byteArray, 0, byteArray.Length);
              }
              txtLog.Text += "Transact_Posting_Core()-Completed Transact Posting Request Stream Process" + Environment.NewLine;

              txtLog.Text += "Transact_Posting_Core()-Started Transact Posting Response Process" + Environment.NewLine;
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();

              StreamReader sr = new StreamReader(response.GetResponseStream());
              string result = sr.ReadToEnd();
              txtResponse.Text = result;
              txtLog.Text += "Transact_Posting_Core()-Completed Transact Posting Response Process" + Environment.NewLine;
Posted
Updated 17-Nov-22 22:20pm
v2

1 solution

The endpoint you're trying to access doesn't support any of the protocols you're trying to use (SSL3, TLS 1.0, TLS 1.1, or TLS 1.2). At a guess, it may only support TLS 1.3, since the others have security vulnerabilities and are often disabled.

Rather than explicitly defining the protocols to use, the recommendation is to use any protocol enabled in the OS. If you need TLS 1.3 support, you'll need to target .NET Framework 4.8.

Transport Layer Security (TLS) best practices with the .NET Framework - .NET Framework | Microsoft Learn[^]
 
Share this answer
 
Comments
Member 7513082 30-Nov-22 11:10am    
I upgraded the application to higher framework and set TLS 1.3 also as below . Both TLS 1.2 & 1.3 are enabled in server. Also a certificate file is added with the request. With and without the certificate getting same above exception.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Tls13
| SecurityProtocolType.Ssl3;
If i try via TLS 1.3 alone I am getting exception
underlying connection was closed an unexpected error occurred on a receive
I am getting the response via Postman though .
Richard Deeming 30-Nov-22 11:18am    
As I keep telling you, don't explicitly configure the ServicePointManager.SecurityProtocol; let the operating system decide which protocols are enabled.

If you're going to keep ignoring the advice you're given, then there's no way to help you.
Member 7513082 30-Nov-22 22:20pm    
I have given the option to call below security protocols and now the issue is that we are getting the exception of request was not aborted. could not create ssl or tls channel. Response is coming via Postman app though.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Tls13
| SecurityProtocolType.Ssl3;
Richard Deeming 1-Dec-22 3:23am    
You're determined to ignore any advice given to you. There is no way to help you, and there is no point you posting anything further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900