C#模拟Http与Https请求框架类实例
本文实例讲述了c#模拟http与https请求框架类。分享给大家供大家参考。
具体实现方法如下:
using system.text;
using system.net;
using system.io;
using system.text.regularexpressions;
using system.security.cryptography.x509certificates;
using system.net.security;
namespace webrequesttest
{
/// <summary>
/// 动态类,每个实例使用单独session
/// </summary>
public class httphelpernew
{
public cookiecontainer cookie = new cookiecontainer();
/// <summary>
/// post请求返回html
/// </summary>
/// <param name="url"></param>
/// <param name="postdatastr"></param>
/// <returns></returns>
public string httppost(string url, string postdatastr)
{
httpwebrequest request = (httpwebrequest)webrequest.create(url);
//request.allowautoredirect = false; //禁止自动重定向
request.method = "post";
request.contenttype = "application/x-www-form-urlencoded";
request.contentlength = encoding.utf8.getbytecount(postdatastr);
request.cookiecontainer = cookie; //cookie信息由cookiecontainer自行维护
stream myrequeststream = request.getrequeststream();
streamwriter mystreamwriter = new streamwriter(myrequeststream, encoding.getencoding("gb2312"));
mystreamwriter.write(postdatastr);
mystreamwriter.close();
httpwebresponse response = null;
try
{
this.setcertificatepolicy();
response = (httpwebresponse)request.getresponse();
}
catch (system.exception ex)
{
}
//获取重定向地址
//string url1 = response.headers["location"];
if (response !=null)
{
stream myresponsestream = response.getresponsestream();
streamreader mystreamreader = new streamreader(myresponsestream, encoding.getencoding("utf-8"));
string retstring = mystreamreader.readtoend();
mystreamreader.close();
myresponsestream.close();
return retstring;
}
else
{
return "error"; //post请求返回为空
}
}
/// <summary>
/// get请求获取返回的html
/// </summary>
/// <param name="url"></param>
/// <param name="postdatastr"></param>
/// <returns></returns>
public string httpget(string url, string querydata)
{
httpwebrequest request = (httpwebrequest)webrequest.create(url + (querydata == "" ? "" : "?") + querydata);
request.method = "get";
request.contenttype = "text/html;charset=utf-8";
request.cookiecontainer = cookie;
this.setcertificatepolicy();
httpwebresponse response = (httpwebresponse)request.getresponse();
// response.cookies = cookie.getcookies(response.responseuri);
stream myresponsestream = response.getresponsestream();
streamreader mystreamreader = new streamreader(myresponsestream, encoding.getencoding("utf-8"));
string retstring = mystreamreader.readtoend();
mystreamreader.close();
myresponsestream.close();
return retstring;
}
/// <summary>
/// 获得响应中的图像
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public stream getresponseimage(string url)
{
stream resst = null;
try
{
httpwebrequest req = (httpwebrequest)webrequest.create(url);
req.keepalive = true;
req.method = "get";
req.allowautoredirect = true;
req.cookiecontainer = cookie;
req.contenttype = "application/x-www-form-urlencoded";
req.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.timeout = 50000;
encoding myencoding = encoding.getencoding("utf-8");
this.setcertificatepolicy();
httpwebresponse res = (httpwebresponse)req.getresponse();
resst = res.getresponsestream();
return resst;
}
catch
{
return null;
}
}
/// <summary>
/// 正则获取匹配的第一个值
/// </summary>
/// <param name="html"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public string getstringbyregex(string html,string pattern)
{
regex re = new regex(pattern, regexoptions.ignorecase);
matchcollection matchs = re.matches(html);
if (matchs.count > 0)
{
return matchs[0].groups[1].value;
}
else
return "";
}
/// <summary>
/// 正则验证返回的response是否正确
/// </summary>
/// <param name="html"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public bool verifyresponsehtml(string html ,string pattern)
{
regex re = new regex(pattern);
return re.ismatch(html);
}
//注册证书验证回调事件,在请求之前注册
private void setcertificatepolicy()
{
servicepointmanager.servercertificatevalidationcallback
+= remotecertificatevalidate;
}
/// <summary>
/// 远程证书验证,固定返回true
/// </summary>
private static bool remotecertificatevalidate(object sender, x509certificate cert,
x509chain chain, sslpolicyerrors error)
{
return true;
}
}
}
希望本文所述对大家的c#程序设计有所帮助。