Is that possible to send HttpWebRequest using TLS1.2 on .NET 4.0 framework

Deadly 提交于 2019-11-27 13:53:16
Crowcoder

Yes, it supports it but you must explicitly set the TLS version on the ServicePointManager. Just have this code run anytime (in same app domain) before you make the call to Experian:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12

Update

see @iignatov 's answer for what you must do for framework v4.0. My code works with 4.5+

I had to deal with the same problem, while integrating PayPal into a legacy application, and found the following workaround for .NET 4.0 which seems to do the trick:

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.DefaultConnectionLimit = 9999;

Basically the workaround is to directly assign the port for TLS 1.2.

All credit goes to the commenter at CodeProject.

Matt G

The VB.NET Translation of iignatov's answer:

ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
ServicePointManager.DefaultConnectionLimit = 9999

Unfortunately no, you can't do this. Tls12 was not added until .netfx 4.5 (see the documentation). Note this also requires Windows Server 2008 R2+ or Windows 7+ to run correctly (notice the Applies To section on Introducing TLS).

FrameWork 4.0 does not support TLS 1.1 or 1.2 But you can fix this problem by downloading Rebex.Http from Nuget manager.

Rebex.Licensing.Key = "..."; //Lisans Number
var creator = new HttpRequestCreator();
creator.Register();

WebRequest request = WebRequest.Create("https://www.test.com");
request.Method = "POST";                
request.Headers.Add("utsToken", txtToken.Text);
request.ContentType = "application/json";
request.Method = "POST";

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string json = "{\"VRG\":\"test\"}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

var httpResponse = (WebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    txtSonuc.Text += result;
}

You can also use this:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!