• c# 请求 HTTPS


     1 private static string GetUrl(string url)
     2 {
     3 HttpWebRequest request = null;
     4 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
     5 {
     6 request = WebRequest.Create(url) as HttpWebRequest;
     7 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
     8 request.ProtocolVersion = HttpVersion.Version11;
     9 // 这里设置了协议类型。
    10 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
    11 request.KeepAlive = false;
    12 ServicePointManager.CheckCertificateRevocationList = true;
    13 ServicePointManager.DefaultConnectionLimit = 100;
    14 ServicePointManager.Expect100Continue = false;
    15 }
    16 else
    17 {
    18 request = (HttpWebRequest)WebRequest.Create(url);
    19 }
    20 
    21 request.Method = "GET"; //使用get方式发送数据
    22 request.ContentType = "application/x-www-form-urlencoded";
    23 request.Referer = null;
    24 request.AllowAutoRedirect = true;
    25 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    26 request.Accept = "*/*";
    27 
    28 //byte[] data = Encoding.UTF8.GetBytes(postData);
    29 //Stream newStream = request.GetRequestStream();
    30 //newStream.Write(data, 0, data.Length);
    31 //newStream.Close();
    32 
    33 //获取网页响应结果
    34 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    35 Stream stream = response.GetResponseStream();
    36 //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    37 string result = string.Empty;
    38 using (StreamReader sr = new StreamReader(stream))
    39 {
    40 result = sr.ReadToEnd();
    41 }
    42 
    43 return result;
    44 }
    45 
    46 private static string PostUrl(string url, string postData)
    47 {
    48 HttpWebRequest request = null;
    49 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
    50 {
    51 request = WebRequest.Create(url) as HttpWebRequest;
    52 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
    53 request.ProtocolVersion = HttpVersion.Version11;
    54 // 这里设置了协议类型。
    55 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
    56 request.KeepAlive = false;
    57 ServicePointManager.CheckCertificateRevocationList = true;
    58 ServicePointManager.DefaultConnectionLimit = 100;
    59 ServicePointManager.Expect100Continue = false;
    60 }
    61 else
    62 {
    63 request = (HttpWebRequest)WebRequest.Create(url);
    64 }
    65 
    66 request.Method = "POST"; 
    67 request.ContentType = "application/x-www-form-urlencoded";
    68 request.Referer = null;
    69 request.AllowAutoRedirect = true;
    70 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    71 request.Accept = "*/*";
    72 
    73 byte[] data = Encoding.UTF8.GetBytes(postData);
    74 Stream newStream = request.GetRequestStream();
    75 newStream.Write(data, 0, data.Length);
    76 newStream.Close();
    77 
    78 //获取网页响应结果
    79 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    80 Stream stream = response.GetResponseStream();
    81 //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    82 string result = string.Empty;
    83 using (StreamReader sr = new StreamReader(stream))
    84 {
    85 result = sr.ReadToEnd();
    86 }
    87 
    88 return result;
    89 }
    90 
    91 private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    92 {
    93 return true; //总是接受 
    94 }
    View Code
  • 相关阅读:
    windows :Tomcat免安装版环境变量配置 + jdk配置
    如何在官网下载Spring jar包
    浅析win32 Win64 x86 x64 区别 及Eclipse启动报Java was started but returned exit code=13 错误
    MyBatis拦截器打印不带问号的完整sql语句方法
    MyBatis多个接口参数报错:Available parameters are [0, 1, param1, param2], 及解决方法
    Leetcode40--->Combination Sum II
    Leetcode39--->Combination Sum(在数组中找出和为target的组合)
    Leetcode38--->Count and Say
    js 保留小数位数
    如何禁用easyui-linkbutton 中的Click事件
  • 原文地址:https://www.cnblogs.com/danmoqingshan/p/9316073.html
Copyright © 2020-2023  润新知