阅读导航
---------------------------------------------------------------------------------------------------------------
C#Httphelper类下载
更新报告
---------------------------------------------------------------------------------------------------------------
这个跟以前的相比使用上并没有多大变化
首先修改一下HttpItem类增加如下代码
View Code
string _CerPath = string.Empty; /// <summary> /// 证书绝对路径 /// </summary> public string CerPath { get { return _CerPath; } set { _CerPath = value; } }[/code] 大家看一下验证代码 [code=csharp] #region 验证证书 if (!string.IsNullOrEmpty(objhttpItem.CerPath)) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(GetUrl(objhttpItem.URL)); //创建证书文件 X509Certificate objx509 = new X509Certificate(objhttpItem.CerPath); //添加到请求里 request.ClientCertificates.Add(objx509); } else { //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(GetUrl(objhttpItem.URL)); } #endregion
CheckValidationResult方法如下:
//回调验证证书问题 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // 总是接受 return true; }
例子
---------------------------------------------------------------------------------------------------------------
这个和以前的使用方法没有多大分别,只是加了一个参数看下面方法
objHttpItem.URL ="https://cckan.net"; objHttpItem.Encoding = "gb2312"; objHttpItem.Method = "GET"; objHttpItem.Referer = "http://cckan.net"; objHttpItem.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1"; objHttpItem.ContentType = "text/html"; objHttpItem.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; //objHttpItem.CerPath = "D:\\123.cer"; //登录cckan.net string html = objhttp.GetHtml(objHttpItem); //取得登录后的Cookie objHttpItem.Cookie = objhttp.cookie;