In Console Application, while trying to hit the "https" Endpoint configured with TLS 1.2.
In C# While using HttpClient I am getting the success response from endpoint
HttpClient httpClient = new HttpClient();
//specify to use TLS 1.2 as default connection
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
httpClient.BaseAddress = new Uri("HTTPSENDPOINT");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent("POSTDATA");
var task = httpClient.PostAsync("/Token", content);
task.Wait();
Console.WriteLine(task.Result.Content.ReadAsStringAsync().Result.ToString());
But when using HttpWebRequest
var request = (HttpWebRequest)WebRequest.Create("HTTPSENDPOINT/Token");
var postData = "POSTDATA";
var data = Encoding.ASCII.GetBytes(postData);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream()) // Getting Error in GetRequestStream() method call
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
I am getting below error
The request was aborted: Could not create SSL/TLS secure channel.
Please guide me what I am doing wrong while using HttpWebRequest?
SecurityProtocol
before callingWebRequest.Create
. – Fredella