码迷,mamicode.com
首页 > Windows程序 > 详细

c# 请求 HTTPS

时间:2018-07-16 11:30:07      阅读:352      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!