• WIn7 下使用 NetHttpClient 请求HTPPS 网站


    在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()+"
    ");
                var re = response.Content.ReadAsStringAsync();
                data =re.Result;
                textBox1.AppendText(data);
            }
    C# 代码
  • 相关阅读:
    学点 C 语言(40): 函数 多参函数
    存取 ListBox 列表 回复 "徐强" 的问题
    博客园RSS订阅汇总
    博客园电子期刊2012年2月刊发布啦
    上周热点回顾(3.53.11)
    博客园电子期刊2012年3月刊发布啦
    上周热点回顾(3.264.1)
    上周热点回顾(3.193.25)
    上周热点回顾(4.24.8)
    上周热点回顾(2.273.4)
  • 原文地址:https://www.cnblogs.com/stroll/p/12987796.html
Copyright © 2020-2023  润新知