參考路徑:https://www.cnblogs.com/splendidme/archive/2011/09/14/2175364.htmlhtml
1 /// <summary> 2 /// 類說明:HttpHelps類,用來實現Http訪問,Post或者Get方式的,直接訪問,帶Cookie的,帶證書的等方式 3 /// 編碼日期:2011-09-13 4 5 /// </summary>using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Web; 9 using System.Text; 10 using System.Net; 11 using System.IO; 12 using System.Security.Cryptography.X509Certificates; 13 using System.Net.Security; 14 using System; 15 16 /// <summary> 17 ///HttpHelpers 主要是實現Http方式的Get和Post方式請求 18 /// </summary> 19 public class HttpHelpers 20 { 21 /// <summary> 22 /// 構造器實現默認屬性的賦值 23 /// </summary> 24 public HttpHelpers() 25 { 26 //設置請求方式爲Post 27 request.Method = "GET"; 28 request.Accept = "text/html, application/xhtml+xml, */*"; 29 request.ContentType = "application/x-www-form-urlencoded"; 30 } 31 32 #region 全部的屬性 33 34 //訪問的頁面地址 35 private string RequestURl { get; set; } 36 37 //默認的編碼 38 private Encoding encoding { get; set; } 39 40 //HttpWebRequest對象用來發起請求 41 private HttpWebRequest request { get; set; } 42 43 //獲取影響流的數據對象 44 private HttpWebResponse response { get; set; } 45 46 //證書文件X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer"); 47 X509Certificate objx509 { get; set; } 48 49 //請求方式目前只提供Post和Get方式 50 private string Method { get; set; } 51 52 //Accept屬性 53 private string Accept { get; set; } 54 55 //ContentType屬性 56 private string ContentType { get; set; } 57 58 //UserAgent屬性 59 private string UserAgent { get; set; } 60 61 //Cookie列表 62 private CookieContainer cookie { get; set; } 63 64 //須要返回的數據對象 65 private string reutrnDate { get; set; } 66 67 //Post數據串 68 private string strPostdata { get; set; } 69 70 #endregion 71 72 #region 內部方法 73 74 //回調驗證證書問題 75 private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 76 { 77 // 老是接受 78 return true; 79 } 80 81 /// <summary> 82 /// 根據相傳入的數據,獲得相應頁面數據 83 /// </summary> 84 /// <param name="strPostdata">傳入的數據Post方式,get方式傳NUll或者空字符串均可以</param> 85 /// <returns>string類型的響應數據</returns> 86 private string GetHttpRequestData() 87 { 88 try 89 { 90 //是否要添加證書驗證 91 if (objx509 != null) 92 { 93 //這一句必定要寫在建立鏈接的前面。使用回調的方法進行證書驗證。 94 ServicePointManager.ServerCertificateValidationCallback = 95 new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 96 } 97 98 //建立request對象 99 request = (HttpWebRequest)WebRequest.Create(RequestURl); 100 101 //是否要添加證書驗證 102 if (objx509 != null) 103 request.ClientCertificates.Add(objx509); 104 105 //是否帶有Cookie值 106 if (cookie != null) 107 request.CookieContainer = cookie; 108 109 //當爲post提交是須要填充數據 110 if (Method.Trim().ToLower() == "post") 111 { 112 byte[] buffer = encoding.GetBytes(strPostdata); 113 request.ContentLength = buffer.Length; 114 request.GetRequestStream().Write(buffer, 0, buffer.Length); 115 } 116 117 //獲得請求的response 118 using (response = (HttpWebResponse)request.GetResponse()) 119 { 120 //從這裏開始咱們要無視編碼了 121 if (encoding == null) 122 GetEonding(); 123 124 //開始讀取流並設置編碼方式 125 using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) 126 { 127 //從當前開始讀取整個流數據,默認爲0因此讀取的是所有,並返回數據 128 reutrnDate = reader.ReadToEnd().ToString().Trim(); 129 } 130 } 131 } 132 catch (Exception) 133 { 134 //這裏是在發生異常時返回的錯誤信息 135 reutrnDate = "String Error"; 136 } 137 return reutrnDate; 138 } 139 140 /// <summary> 141 /// 獲得response對象的編碼類型 142 /// </summary> 143 private void GetEonding() 144 { 145 if (response.CharacterSet.Trim() != "") 146 encoding = System.Text.Encoding.GetEncoding(response.CharacterSet.Trim()); 147 else 148 encoding = System.Text.Encoding.UTF8; 149 } 150 151 #endregion 152 153 #region 公開方法 154 155 /// <summary> 156 /// 只設置一些簡單參數的方式 157 /// </summary> 158 /// <param name="_url">URl地址</param> 159 /// <param name="_strPostdata">Post請求方式時傳入的數據</param> 160 /// <param name="_Method">請求方式GET或者POST能夠爲空默認爲GET</param> 161 /// <param name="_encoding">編碼方式能夠爲空默認爲UTF-8</param> 162 /// <param name="_Accept">Accept屬性</param> 163 /// <param name="_ContentType">ContentType屬性</param 164 /// <param name="_UserAgent">UserAgent屬性</param 165 /// <param name="_cookie">CookieContainer列表</param 166 /// <param name="_objx509">X509Certificate證書對象</param 167 /// <returns>請求所獲得的數據</returns> 168 public string GetString_null(string _url, string _strPostdata, string _Method, Encoding _encoding, string _Accept, 169 string _ContentType, string _UserAgent, CookieContainer _cookie, X509Certificate _objx509) 170 { 171 RequestURl = _url; 172 Method = _Method; 173 encoding = _encoding; 174 Accept = _Accept; 175 ContentType = _ContentType; 176 UserAgent = _UserAgent; 177 cookie = _cookie; 178 objx509 = _objx509; 179 return GetHttpRequestData(); 180 } 181 182 /// <summary> 183 /// 只設置一些簡單參數的方式 184 /// </summary> 185 /// <param name="_url">URl地址</param> 186 /// <param name="_strPostdata">Post請求方式時傳入的數據</param> 187 /// <param name="_Method">請求方式GET或者POST能夠爲空默認爲GET</param> 188 /// <param name="_encoding">編碼方式能夠爲空默認爲UTF-8</param> 189 /// <returns>請求所獲得的數據</returns> 190 public string GetString_type(string _url, string _Method, Encoding _encoding) 191 { 192 RequestURl = _url; 193 Method = _Method; 194 encoding = _encoding; 195 return GetHttpRequestData(); 196 } 197 198 //下面你們本身能夠多寫幾種經常使用的,呵呵 199 200 #endregion 201 202 }