windows10 .NET http请求 tls1.3问题
目标网站
问题描述及尝试
-
使用.net(任何版本,http无法请求,异常:
The SSL connection could not be established, see inner exception.
因为算法不同,客户端和服务器无法通信。
使用下面代码同样无效
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13; -
使用浏览器打开,开启Fiddler后,显示连接被重置,无法打开。log:
HTTPS handshake to www.szzy.edu.cn (for #84) failed. System.Security.Authentication.AuthenticationException 调用 SSPI 失败,请参见内部 -
使用 C:\Windows\System32\curl.exe 无法请求
-
使用C:\Program Files\Git\mingw64\bin\curl.exe 请求成功。
StackOverflow上的信息:
因为 TLS 1.3 客户端仅在 Windows 11/Server 2022 上受支持,所以您遇到的是操作系统级别的错误。
最有可能的是,您的机器正在发送 TLS 握手,指定它支持的最大 TLS 版本是 1.2,但在这里失败,因为服务器需要 1.3,所以您得到“HandshakeFailure”。
解决方法
使用curlsharp,从 https://curl.se/windows/下载 libcurl.dll、curl-ca-bundle.crt 复制到根目录,目前仅测试用32位成功
注意:要指定证书 easy.CaInfo = "curl-ca-bundle.crt";
参考代码
using System;
using System.Linq;
using System.Text;
using CurlSharp;
namespace EasyGet
{
internal class EasyGet
{
public static void Main(String[] args)
{
try
{
Curl.GlobalInit(CurlInitFlag.All);
using (var easy = new CurlEasy())
{
easy.Url = "https://www.szzy.edu.cn/1359/list.aspx";
easy.WriteData = null;
easy.WriteFunction = OnWriteData;
//easy.ProgressFunction = OnProgressData;
easy.CaInfo = "curl-ca-bundle.crt";
var code=easy.Perform();
Console.WriteLine(code);
}
Curl.GlobalCleanup();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
//var userData = (string)extraData;
Console.Write(Encoding.UTF8.GetString(buf));
return size * nmemb;
}
public static int OnProgressData(Object extraData, double dlTotal, double dlNow, double ulTotal, double ulNow)
{
Console.WriteLine("{0} / {1}", dlNow, dlTotal);
return 0;
}
}
}
浙公网安备 33010602011771号