簡體   English   中英

是否可以在 .NET 4.0 框架上使用 TLS1.2 發送 HttpWebRequest

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

我的應用程序連接到 Experian 服務器,Experian 將很快停止支持 TLS 1.0 和 TLS 1.1。 所有使用 HTTPS 的連接都必須使用 TLS 版本 1.2。

我想對這個問題做一些研究,看看在 .NET 4.0 框架上使用 TLS 1.2 發送HttpWebRequest

如果沒有,我可能需要在 .NET 4.5 上創建一個webservice服務並調用它的方法,如果有,我什么都不用做。

有沒有人已經遇到過這個問題?

是的,它支持它,但您必須在ServicePointManager上明確設置 TLS 版本。 只需在調用 Experian 之前隨時運行此代碼(在同一應用程序域中):

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

更新

請參閱@iignatov 對框架 v4.0 必須執行的操作的回答 我的代碼適用於 4.5+

我不得不處理同樣的問題,同時將 PayPal 集成到遺留應用程序中,並找到了以下適用於 .NET 4.0 的解決方法,這似乎可以解決問題:

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

基本上解決方法是直接為 TLS 1.2 分配端口。

所有功勞歸於 CodeProject的評論者。

iignatov 的答案的 VB.NET 翻譯:

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

我用這種方法解決了。

    string url = "https://api.foursquare.com/v2/blablabla...";
    var request = (HttpWebRequest)WebRequest.Create(url);

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    
    var response = (HttpWebResponse)request.GetResponse();
    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

你也可以使用這個:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;

不幸的是,不,你不能這樣做。 直到 .netfx 4.5 才添加 Tls12(請參閱文檔)。 請注意,這還需要 Windows Server 2008 R2+ 或 Windows 7+ 才能正確運行(請注意介紹 TLS的適用於部分)。

FrameWork 4.0 不支持 TLS 1.1 或 1.2 但您可以通過從 Nuget 管理器下載Rebex.Http來解決此問題。

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;
}

我的應用程序連接到Experian服務器,Experian將很快停止支持TLS 1.0和TLS 1.1。 使用HTTPS的所有連接都必須使用TLS版本1.2。

我想對該問題進行一些研究,並看到在.NET 4.0框架上使用TLS 1.2發送HttpWebRequest工作原理

如果沒有,我可能將需要在.NET 4.5上創建一個webservice服務並調用其方法,如果需要,則無需執行任何操作。

有沒有人已經面對過這個問題?

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

這在.net 3.5上對我有用

你不能,

正如消息來源所解釋的那樣,

“HttpWebRequest class 僅支持 HTTP 的版本 1.0 和 1.1。將 ProtocolVersion 設置為不同的版本會引發異常。”

來源: learn.microsoft.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM