在C#開發中常常須要進行Web遠程訪問,方法不少,也使用了好久,但一直沒有作一個總結。算法
C#中用來進行遠程請求的方法有不少,如WebClient,WebRequest等,也各有特色。今天在這裏主要介紹WebRequest。瀏覽器
先從相對入門的不須要證書驗證,不須要登陸的開始。服務器
1 //Get 2 public string GetContent(string uri, Encoding coding) 3 { 4 //Get請求中請求參數等直接拼接在url中 5 WebRequest request = WebRequest.Create(uri); 6 7 //返回對Internet請求的響應 8 WebResponse resp = request.GetResponse(); 9 10 //從網絡資源中返回數據流 11 Stream stream = resp.GetResponseStream(); 12 13 StreamReader sr = new StreamReader(stream, coding); 14 15 //將數據流轉換文字符串 16 string result = sr.ReadToEnd(); 17 18 //關閉流數據 19 stream.Close(); 20 sr.Close(); 21 22 return result; 23 }
上面的方法是GET類型的請求。下面介紹POST請求。網絡
1 //POST 2 public string GetContentPost(string uri, string data, Encoding coding) 3 { 4 WebRequest request = WebRequest.Create(uri); 5 request.ContentType = "application/x-www-form-urlencoded"; 6 request.Method = "POST"; 7 8 //將字符串數據轉化爲字節串,這也是POST請求與GET請求區別的地方 9 byte[] buffer = coding.GetBytes(data); 10 11 //用於將數據寫入Internet資源 12 Stream stream = request.GetRequestStream(); 13 stream.Write(buffer, 0, buffer.Length); 14 request.ContentLength = buffer.Length; 15 16 WebResponse response = request.GetResponse(); 17 18 //從網絡資源中返回數據流 19 stream = response.GetResponseStream(); 20 21 StreamReader sr = new StreamReader(stream, coding); 22 23 //將數據流轉換文字符串 24 string result = sr.ReadToEnd(); 25 26 //關閉流數據 27 stream.Close(); 28 sr.Close(); 29 30 return result; 31 }
上面兩個方法的特色就是簡單易使用,用於獲取HTML文檔。缺點就是沒法處理那些須要證書的頁面。app
下面咱們介紹須要證書驗證的頁面如何請求。函數
1 //回調驗證證書問題 2 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 3 { 4 //直接返回true,接受指定證書進行身份驗證 5 return true; 6 } 7 8 //Get 9 public string GetContent(string uri, Encoding coding) 10 { 11 12 //下面一行代碼必定卸載請求開始前。 13 //使用回調的方法進行驗證。 14 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 15 16 //Get請求中請求參數等直接拼接在url中 17 WebRequest request = WebRequest.Create(uri); 18 19 //返回對Internet請求的響應 20 WebResponse resp = request.GetResponse(); 21 22 //從網絡資源中返回數據流 23 Stream stream = resp.GetResponseStream(); 24 25 StreamReader sr = new StreamReader(stream, coding); 26 27 //將數據流轉換文字符串 28 string result = sr.ReadToEnd(); 29 30 //關閉流數據 31 stream.Close(); 32 sr.Close(); 33 34 return result; 35 } 36 37 //POST 38 public string GetContentPost(string uri, string data, Encoding coding) 39 { 40 //下面一行代碼必定卸載請求開始前。 41 //使用回調的方法進行驗證。 42 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 43 44 WebRequest request = WebRequest.Create(uri); 45 request.ContentType = "application/x-www-form-urlencoded"; 46 request.Method = "POST"; 47 48 //將字符串數據轉化爲字節串,這也是POST請求與GET請求區別的地方 49 byte[] buffer = coding.GetBytes(data); 50 51 //用於將數據寫入Internet資源 52 Stream stream = request.GetRequestStream(); 53 stream.Write(buffer, 0, buffer.Length); 54 request.ContentLength = buffer.Length; 55 56 WebResponse response = request.GetResponse(); 57 58 //從網絡資源中返回數據流 59 stream = response.GetResponseStream(); 60 61 StreamReader sr = new StreamReader(stream, coding); 62 63 //將數據流轉換文字符串 64 string result = sr.ReadToEnd(); 65 66 //關閉流數據 67 stream.Close(); 68 sr.Close(); 69 70 return result; 71 }
咱們以百度舉例,看訪問百度時如何獲取百度服務器的證書相關信息。網站
只需在證書驗證的回調函數中添加幾行代碼便可。url
1 //回調驗證證書問題 2 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 3 { 4 Console.WriteLine("證書的有效日期:" + certificate.GetEffectiveDateString()); 5 Console.WriteLine("證書的到期日期:" + certificate.GetExpirationDateString()); 6 Console.WriteLine("證書格式名稱:" + certificate.GetFormat()); 7 Console.WriteLine("證書辦法機構名稱:" + certificate.Issuer); 8 Console.WriteLine("密鑰算法信息:" + certificate.GetKeyAlgorithm()); 9 Console.WriteLine("證書的公鑰:" + certificate.GetPublicKeyString()); 10 Console.WriteLine("證書序列號:" + certificate.GetSerialNumberString()); 11 // 老是接受 12 return true; 13 }
使用以下GET方式訪問百度。便可獲得截圖效果。 spa
GetContent("https://www.baidu.com/", Encoding.UTF8);
Console.WriteLine(str);code
而後經過瀏覽器導航欄的鎖標識,便可直接查看百度的相關證書信息。
經過對比能夠知道,獲取到的證書徹底正確。因此若是想對訪問的網站有證書檢測,也能夠經過這種方式。避免出現請求被攔截等問題。