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

 

转载于:https://www.cnblogs.com/danmoqingshan/p/9316073.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值