阅读导航
---------------------------------------------------------------------------------------------------------------
C#Httphelper类下载
更新报告
---------------------------------------------------------------------------------------------------------------
这个跟以前的相比使用上并没有多大变化
首先修改一下HttpItem类增加如下代码
[C#] 纯文本查看 复制代码 string _CerPath = string.Empty;
/// <summary>
/// 证书绝对路径
/// </summary>
public string CerPath
{
get { return _CerPath; }
set { _CerPath = value; }
}
大家看一下验证代码
[C#] 纯文本查看 复制代码 #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方法如下:
[C#] 纯文本查看 复制代码 //回调验证证书问题
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
// 总是接受
return true;
}
例子
---------------------------------------------------------------------------------------------------------------
这个和以前的使用方法没有多大分别,只是加了一个参数看下面方法
[C#] 纯文本查看 复制代码 objHttpItem.URL ="https://sufeinet.com";
objHttpItem.Encoding = "gb2312";
objHttpItem.Method = "GET";
objHttpItem.Referer = "http://sufeinet.com";
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";
//登录sufeinet.com
string html = objhttp.GetHtml(objHttpItem);
//取得登录后的Cookie
objHttpItem.Cookie = objhttp.cookie;
|