This project is read-only.
HttpUtility库是对C#中HttpRequest(HttpWebRequest)的高度封装。
通过HttpUtility你可以完成如下任务
1.模拟HttpRequest的Get请求,返回请求的html结果
2.模拟HttpRequest的Post提交,返回请求的html结果
3.模拟HttpRequest请求一幅图片,以base64位字符串返回
4.模拟有先后顺序的多个HttpRequest请求

比如要请求博客园首页的html:
BaseRequest request = HttpRequestFactory.CreateHttpRequest("http://www.cnblogs.com/", HttpRequestFactory.HttpMethod.Get);
request.Encoding = "UTF-8";
request.Start();
string html = request.ResponseHtml;

核心类介绍:
抽象基类:BaseRequest;它包含3个扩展类:GetRequest、PostRequest、ImageRequest。
BaseRequest制造工厂类:RequestFactory,它负责创建BaseRequest的各种实现类。
HttpRequestTask请求任务类:它负责创建和管理一次任务包含多个Request请求的情况。

比如,你要以HttpRequest方式模拟163发送邮件,那么最少包含N步:
1.模拟登陆;2.模拟请求写信请求;3.模拟提交邮件... ...
他们之间是依次连通的,那么我们就需要通过HttpRequestTask来管理本次发信任务。
而如果是单次请求的情况,则HttpRequestTask是可选的。


模拟Post提交
string url = "http://xxxxxxxxxxxxxxxxxxxxxxx";


BaseRequest loginRequest = HttpRequestFactory.CreateHttpRequest(url, HttpRequestFactory.HttpMethod.Post);

loginRequest.Encoding = "GB2312";
loginRequest.BeforePost += (request) =>
{
BaseRequest login1Request = HttpRequestFactory.CreateHttpRequest(url, HttpRequestFactory.HttpMethod.Get);
login1Request.Encoding = "GB2312";
login1Request.CookieContainer = request.CookieContainer;
login1Request.Start();
string datas = HtmlHelper.GetFormDataString(login1Request.ResponseHtml);
request.AttachParameters(datas);
request.AddParameter("txtName", "jiasm");
request.AddParameter("txtPwd", "******");
request.AddParameter("ibLogin.x", "56");
request.AddParameter("ibLogin.y", "7");
};
loginRequest.CheckResultSuccess += (html) =>
{
string successKey1 = "http://xxxxxxxxxxxxxxxxxxxxxx";
string successKey2 = @"window.location.href=""Index.aspx""";
if (html.Contains(successKey1) || html.Contains(successKey2))
{
return true;
}
return false;
};

var requestlist = new SortedList<int, BaseRequest>();
requestlist.Add(1, loginRequest);
HttpRequestTask task = new HttpRequestTask(requestlist);

bool result = task.Start();

Last edited Jun 20, 2013 at 1:55 AM by jiasongmao, version 3