学位证书验证,HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式

之前我写过篇关于C# HttpWebRequest 绝技的文章
最近把上面的方法整理了一下,给大家分享一下吧,不好的地方感谢大家留言指正,不多说了上代码吧!
/// /// 类说明:HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式 /// 编码日期:2011-09-13 /// 编 码 人: 苏飞 /// 联系方式:361983679 Email:[email protected] Blogs:http://sufei.cnblogs.com /// using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Net; using System.IO; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System; /// ///HttpHelpers 主要是实现Http方式的Get和Post方式请求 /// public class HttpHelpers { /// /// 构造器实现默认属性的赋值 /// public HttpHelpers() { //设置请求方式为Post request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/x-www-form-urlencoded"; } #region 所有的属性 //访问的页面地址 private string RequestURl { get; set; } //默认的编码 private Encoding encoding { get; set; } //HttpWebRequest对象用来发起请求 private HttpWebRequest request { get; set; } //获取影响流的数据对象 private HttpWebResponse response { get; set; } //证书文件X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer"); X509Certificate objx509 { get; set; } //请求方式目前只提供Post和Get方式 private string Method { get; set; } //Accept属性 private string Accept { get; set; } //ContentType属性 private string ContentType { get; set; } //UserAgent属性 private string UserAgent { get; set; } //Cookie列表 private CookieContainer cookie { get; set; } //需要返回的数据对象 private string reutrnDate { get; set; } //Post数据串 private string strPostdata { get; set; } #endregion #region 内部方法 //回调验证证书问题 private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // 总是接受 return true; } /// /// 根据相传入的数据,得到相应页面数据 /// /// 传入的数据Post方式,get方式传NUll或者空字符串都可以
/// string类型的响应数据 private string GetHttpRequestData() { try { //是否要添加证书验证 if (objx509 != null) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); } //创建request对象 request = (HttpWebRequest)WebRequest.Create(RequestURl); //是否要添加证书验证 if (objx509 != null) request.ClientCertificates.Add(objx509); //是否带有Cookie值 if (cookie != null) request.CookieContainer = cookie; //当为post提交是需要填充数据 if (Method.Trim().ToLower() == "post") { byte[] buffer = encoding.GetBytes(strPostdata); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } //得到请求的response using (response = (HttpWebResponse)request.GetResponse()) { //从这里开始我们要无视编码了 if (encoding == null) GetEonding(); //开始读取流并设置编码方式 using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { //从当前开始读取整个流数据,默认为0所以读取的是全部,并返回数据 reutrnDate = reader.ReadToEnd().ToString().Trim(); } } } catch (Exception) { //这里是在发生异常时返回的错误信息 reutrnDate = "String Error"; } return reutrnDate; } /// /// 得到response对象的编码类型 /// private void GetEonding() { if (response.CharacterSet.Trim() != "") encoding = System.Text.Encoding.GetEncoding(response.CharacterSet.Trim()); else encoding = System.Text.Encoding.UTF8; } #endregion #region 公开方法 /// /// 只设置一些简单参数的方式 /// /// URl地址
/// Post请求方式时传入的数据
/// 请求方式GET或者POST可以为空默认为GET
/// 编码方式可以为空默认为UTF-8
/// Accept属性
/// ContentType属性
请求所得到的数据 public string GetString_null(string _url, string _strPostdata, string _Method, Encoding _encoding, string _Accept, string _ContentType, string _UserAgent, CookieContainer _cookie, X509Certificate _objx509) { RequestURl = _url; Method = _Method; encoding = _encoding; Accept = _Accept; ContentType = _ContentType; UserAgent = _UserAgent; cookie = _cookie; objx509 = _objx509; return GetHttpRequestData(); } /// /// 只设置一些简单参数的方式 /// /// URl地址
/// Post请求方式时传入的数据
/// 请求方式GET或者POST可以为空默认为GET
/// 编码方式可以为空默认为UTF-8
/// 请求所得到的数据 public string GetString_type(string _url, string _Method, Encoding _encoding) { RequestURl = _url; Method = _Method; encoding = _encoding; return GetHttpRequestData(); } //下面大家自己可以多写几种常用的,呵呵 #endregion }

Tags:  上海学历证书验证 无线网络验证证书 学历证书验证 证书验证 学位证书验证

延伸阅读

最新评论

发表评论