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

后端 未结 6 1863
长发绾君心
长发绾君心 2020-11-30 06:27

My application connects to Experian server and Experian will soon stop supporting TLS 1.0 and TLS 1.1. All connectivity using HTTPS must use TLS Version 1.2.

I want

相关标签:
6条回答
  • 2020-11-30 06:53

    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+

    0 讨论(0)
  • 2020-11-30 06:54

    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).

    0 讨论(0)
  • 2020-11-30 06:56

    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;
    }
    
    0 讨论(0)
  • 2020-11-30 07:07

    You can also use this:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    
    0 讨论(0)
  • 2020-11-30 07:08

    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.

    0 讨论(0)
  • 2020-11-30 07:11

    The VB.NET Translation of iignatov's answer:

    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
    ServicePointManager.DefaultConnectionLimit = 9999
    
    0 讨论(0)
提交回复
热议问题