SSL/TLS error occurs when sending http request to Helium LoraWAN Server with restsharp (106.6.1) or httpwebrequest in .net 4.8

40 views Asked by At

I send it as an http request to the Helium LoraWAN server with .net 4.8 version. But I get SSL/TLS error. Error message: The request was aborted: Could not create SSL/TLS secure channel.

There was no such error a while ago. How can such an error occur later and how can I solve this situation?

This process was tried many times on different networks and the same error was encountered in all of them.

Note: When the same request is sent with postman and mozilla restClient, no error occurs. The operation is completed successfully.

with HttpWebRequest

ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;

RequestObj reqObj = new RequestObj
{
    ..
        ..
};
string jsonContent = JsonConvert.SerializeObject(reqObj);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(jsonContent);
request.ContentLength = byteArray.Length;
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;

try
{
    using (Stream dataStream = request.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            string responseBody = reader.ReadToEnd();
            Console.WriteLine("HttpWebRequest response => " + 
                        response.StatusCode.ToString() + ") => " + responseBody);
        }
    }
}

catch (WebException ex)
{
    if (ex.Response != null)
    {
        using (Stream stream = ex.Response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            string errorResponse = reader.ReadToEnd();
            Console.WriteLine("HttpWebRequest errorResponse=>" + errorResponse);
        }
    }
    else
    {
        Console.WriteLine("HttpWebRequest ex.Message => " + ex.Message);
    }
}

with RestSharp

try
{
       ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault |
       SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 |
       SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
       ServicePointManager.Expect100Continue = true;
       ServicePointManager.DefaultConnectionLimit = 9999;
    
        RequestObj reqObj = new RequestObj
        {
         ..
             ..
        };
    string jsonContent = JsonConvert.SerializeObject(reqObj);

    var request = new RestRequest(Method.POST);
    request.AddJsonBody(jsonContent);
    request.AddHeader("Content-Type", "application/json");
    var client = new RestClient(url);
    var response = client.Execute(request);
    Console.WriteLine("RestSharp response.Content => " + response.Content + "\n\nRestSharp 
        response.ErrorMessage => " + response.ErrorMessage); // + "\n\nRestSharp response => " +
        JsonConvert.SerializeObject(response));
}
catch (Exception ex)
{
    Console.WriteLine("RestSharp Req ex => ", ex);
}

Error Message for both => The request was aborted: Could not create SSL/TLS secure channel.

0

There are 0 answers