如何使用WebRequest实现https访问?
摘要:参考代码: 1: [TestMethod] 2: public void TestHttps() 3: { 4: var req =(HttpWebRequest) System.Net.WebRequest.Create(&
参考代码:
1: [TestMethod]
2: public void TestHttps()
3: {
4: var req =(HttpWebRequest) System.Net.WebRequest.Create("https://www.baidu.com/");
5: ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
6: var rep =(HttpWebResponse) req.GetResponse();
7: var stream = rep.GetResponseStream();
8: StreamReader reader = new StreamReader(stream);
9: string content = reader.ReadToEnd();
10: reader.Close();
11: stream.Close();
12: }
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
关键代码:ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
