c# 请求 HTTPS

2021-03-28 20:28

阅读:341

标签:x509   web   hid   ade   img   vat   1.2   one   read   

技术分享图片技术分享图片
 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# 请求 HTTPS

标签:x509   web   hid   ade   img   vat   1.2   one   read   

原文地址:https://www.cnblogs.com/danmoqingshan/p/9316073.html


评论


亲,登录后才可以留言!