HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式
参考路径:https://www.cnblogs.com/splendidme/archive/2011/09/14/2175364.html
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 }