随笔 - 125  文章 - 1 评论 - 9 阅读 - 22万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

在WIN7 下使用 HttpClient 会报以下两种错 ;
1 Server Certificate Invalid or not present
2 Error sending data: (12175) 发生了安全错误.

3 System.Net.WebException: 请求被中止: 未能创建 SSL/TLS 安全通道。

经查询资料是因为WIn7 默认不支持这个协议
详细见:
https://support.microsoft.com/en-us/help/3140245/update-to-enable-tls-1-1-and-tls-1-2-as-default-secure-protocols-in-wi

解决方法:

指定 HttpClient.SecureProtocols  为TLS12

代码:

复制代码
procedure TForm2.Button1Click(Sender: TObject);
var
  str:string;
  ss:TStringStream;//TStreamstring
 begin
   try
    ss:=TStringStream.Create();
    //WIN 7 下使用 需要 指定 默认的协议TLS12
   http.SecureProtocols:=[THTTPSecureProtocol.TLS12];
    http.Get(url,ss);
    memo1.Text:=ss.DataString;
   finally
      ss.Free;
   end;
end;
View Code
复制代码
复制代码
   private void button1_Click(object sender, EventArgs e)
        {
            //ServicePointManager.ServerCertificateValidationCallback =
            //    new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            string data;
            HttpClient http = new HttpClient();
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpResponseMessage response = http.GetAsync("https://kindao1.github.io/LimitTW/").Result;
            textBox1.AppendText(response.ToString()+"\r\n");
            var re = response.Content.ReadAsStringAsync();
            data =re.Result;
            textBox1.AppendText(data);
        }
C# 代码
复制代码

 

posted on   EEEEEEEEEEEEEEEEEEE  阅读(2582)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示