zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    C# IP地址字符串和数值转换
    Xstream序列化实体
    异步线程及同步控制
    XML序列化与反序列化(转)
    C# Webserice 代理生成工具(WSDL)
    ASP.NET(C#)图片加文字、图片水印(转)
    异步导致UI句柄增加的解决办法
    终于找到WinForm自定义控件不能拖到IDE设计器容器的办法
    C# PropertyGrid控件应用心得(转载)
    GDI_图片半透明效果示例
  • 原文地址:https://www.cnblogs.com/danmoqingshan/p/9316073.html
Copyright © 2011-2022 走看看