新年开篇,忙归忙,还是要写点什么,不然越来越懒,分享我写的HttpTooler

    public delegate void RequestCompleted(object sender, string html);
public enum RequestType
{
WebRequest,
HttpClient,
WebClient
} public class HttpTooler
{
private static byte[] postBuffer;
//private HttpWebRequest request;
private const string contentType = "application/x-www-form-urlencoded;charset=utf-8";
private const string multipartType = "multipart/form-data; boundary=";
private const string formBoundary = "----WebKitFormBoundary";
private const string accept = "text/html, application/xhtml+xml, */*";
private const string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
private const string cacheCtr = "Cache-Control";
private const string noCache = "no-cache"; private bool changeFlag;
private string oldDomain;
private string newDomain; public CookieContainer container; public RequestCompleted RequestCompleted
{
private get;
set;
} public HttpTooler()
{
container = new CookieContainer();
} public HttpTooler(CookieContainer container)
{
this.container = container;
} public void FixCookieDomain(string oldstr, string newstr)
{
changeFlag = true;
oldDomain = oldstr;
newDomain = newstr;
} private void GetRequestHtml(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.KeepAlive = true;
request.Timeout = 30000;
request.ContentType = contentType;
request.Accept = accept;
request.UserAgent = userAgent;
request.Headers.Add(cacheCtr, noCache);
request.AllowAutoRedirect = false;
request.CookieContainer = container;
request.KeepAlive = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string html = string.Empty;
using (Stream sw = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(sw))
{
html = reader.ReadToEnd();
} if (changeFlag) FixCookieDomain(response);
}
if (response != null) response.Close(); DoCompleted(html);
} public void GetHtml(RequestType type, string url)
{
if (type == RequestType.WebRequest)
{
GetRequestHtml(url);
}
else if (type == RequestType.WebClient)
{
GetWebClientHtml(url);
}
else
{
GetHttpClientHtml(url);
}
} private void GetWebClientHtml(string url)
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
byte[] data = wc.DownloadData(url);
string html = Encoding.UTF8.GetString(data); DoCompleted(html);
} private void GetHttpClientHtml(string url)
{
HttpClient hc = new HttpClient(container);
hc.Credentials = CredentialCache.DefaultCredentials;
byte[] data = hc.DownloadData(url);
string html = Encoding.UTF8.GetString(data); DoCompleted(html);
} public void PostData(string url, string param, string referer)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
if (string.IsNullOrEmpty(referer)) request.Referer = referer;
request.KeepAlive = true;
request.Timeout = 30000;
request.ContentType = contentType;
request.Accept = accept;
request.UserAgent = userAgent;
request.Headers.Add(cacheCtr, noCache);
request.AllowAutoRedirect = false;
request.CookieContainer = container;
request.KeepAlive = true;
byte[] buff = Encoding.UTF8.GetBytes(param);
Stream reqStream = request.GetRequestStream();
reqStream.Write(buff, 0, buff.Length);
reqStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string html = string.Empty;
using (Stream sw = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(sw))
{
html = reader.ReadToEnd();
} string strcook = response.Headers["Set-Cookie"]; if (changeFlag) FixCookieDomain(response);
}
if (response != null) response.Close(); DoCompleted(html);
} public void PostDataAsync(string url, string param)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.Timeout = 30000;
request.ContentType = contentType;
request.CookieContainer = container;
postBuffer = Encoding.UTF8.GetBytes(param);
request.BeginGetRequestStream(new AsyncCallback(RequestStreamAsync), request);
} private void DoCompleted(string rlt)
{
if (RequestCompleted != null)
{
RequestCompleted(this, rlt);
}
} private void RequestStreamAsync(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
Stream reqStream = request.EndGetRequestStream(result);
reqStream.Write(postBuffer, 0, postBuffer.Length);
reqStream.Close();
request.BeginGetResponse(new AsyncCallback(ResponseAsync), request);
} private void ResponseAsync(IAsyncResult result)
{
HttpWebRequest req = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(result); string html = string.Empty;
using (Stream sw = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(sw))
{
html = reader.ReadToEnd();
}
}
if (response != null) response.Close(); DoCompleted(html);
} public void MultiPostData(string url, string referer, Dictionary<string, string> PostData)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.Timeout = 30000;
string boundary = formBoundary + DateTime.Now.Ticks.ToString("x");
request.ContentType = string.Format("{0}{1}", multipartType, boundary);
request.Accept = accept;
request.UserAgent = userAgent;
request.Headers.Add(cacheCtr, noCache);
request.AllowAutoRedirect = false;
request.CookieContainer = container;
request.ServicePoint.Expect100Continue = false;
if (referer != string.Empty) request.Referer = referer; byte[] buff = BuildMultiPostData(boundary, PostData);
BinaryWriter bw = new BinaryWriter(request.GetRequestStream());
bw.Write(buff);
bw.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string html = string.Empty;
using (Stream sw = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(sw))
{
html = reader.ReadToEnd();
} //第一次提交修改cookie
if (changeFlag) { FixCookieDomain(response); }
}
if (response != null) response.Close(); DoCompleted(html);
} private byte[] BuildMultiPostData(string boundary, Dictionary<string, string> postData)
{
StringBuilder sb = new StringBuilder(); // append access token.
//sb.AppendLine("--" + boundary);
//sb.Append(Environment.NewLine); // append form part.
if (postData != null && postData.Count > 0)
{
foreach (KeyValuePair<string, string> HttpPostDataItem in postData)
{
sb.AppendLine("--" + boundary);
sb.AppendLine(string.Format("Content-Disposition: form-data;name=\"{0}\"", HttpPostDataItem.Key));
sb.Append(Environment.NewLine);
sb.AppendLine(HttpPostDataItem.Value);
}
} MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(Encoding.UTF8.GetBytes(sb.ToString()));
bw.Write(Encoding.UTF8.GetBytes(Environment.NewLine));
bw.Write(Encoding.UTF8.GetBytes("--" + boundary + "--"));
bw.Write(Encoding.UTF8.GetBytes(Environment.NewLine));
ms.Flush();
ms.Position = 0; byte[] result = ms.ToArray(); bw.Close(); return result;
} private void FixCookieDomain(HttpWebResponse response)
{
string strCookie = response.Headers["Set-Cookie"];
CookieCollection collect = GetCookiesFromReponseHead(strCookie, oldDomain);
foreach (Cookie cook in collect)
{
Cookie ncook = new Cookie();
ncook.Domain = oldDomain;
ncook.Name = cook.Name;
ncook.Value = cook.Value;
Cookie pcook = FindCookie(newDomain, cook.Name);
if (pcook == null)
{
container.Add(ncook);
}
else
{
pcook = ncook;
}
} changeFlag = false;
} private CookieCollection GetCookiesFromReponseHead(string setCookieHeadStr, string defaultDomain)
{
CookieCollection cookies = new CookieCollection();
if (!string.IsNullOrEmpty(setCookieHeadStr))
{
setCookieHeadStr = setCookieHeadStr.Replace("HttpOnly,", string.Empty).Replace("httponly", string.Empty);
foreach (string str in setCookieHeadStr.Split(new string[] { "Path=/,", "path=/,", "path=/;", "Path=/;", "PATH=/;" }, StringSplitOptions.None))
{
string cookieString = str.Trim();
if (cookieString.IndexOf(',') == 0)
{
cookieString = cookieString.Substring(1);
}
try
{
Cookie cookieFromString = GetCookieFromString(cookieString, defaultDomain);
if (cookieFromString != null)
{
cookies.Add(cookieFromString);
}
}
catch (Exception exception)
{
//LogManager.Error("GetCookiesFromReponseHead", exception);
}
}
}
return cookies;
} private static Cookie GetCookieFromString(string cookieString, string defaultDomain)
{
Cookie cookie = new Cookie();
string[] strArray = cookieString.Split(new char[] { ';', ',' });
int index = strArray[0].IndexOf("=");
if (index == -1)
{
return null;
}
if (!strArray[0].ToLower().StartsWith("domain=") && !strArray[0].ToLower().StartsWith("expires="))
{
cookie.Name = strArray[0].Substring(0, index);
cookie.Value = strArray[0].Substring(index + 1);
}
else
{
bool flag = false;
if (strArray[0].ToLower().StartsWith("domain=") && strArray[0].Contains(","))
{
strArray[0] = strArray[0].Substring(strArray[0].IndexOf(',') + 1);
index = strArray[0].IndexOf("=");
if (index > -1)
{
cookie.Name = strArray[0].Substring(0, index);
cookie.Value = strArray[0].Substring(index + 1);
flag = true;
}
}
if (!flag)
{
for (int j = 1; j < strArray.Length; j++)
{
index = strArray[j].IndexOf("=");
if (index > 0)
{
string str = strArray[j].Substring(0, index);
if ((!str.ToLower().StartsWith("domain=") && !str.ToLower().StartsWith("expires=")) && !str.ToLower().StartsWith("version="))
{
string str2 = strArray[j].Substring(index + 1);
cookie.Name = str.Replace(",", "").Trim();
cookie.Value = str2;
flag = true;
break;
}
}
}
}
if (!flag)
{
return null;
}
}
Hashtable hashtable = new Hashtable();
for (int i = 0; i < strArray.Length; i++)
{
if (!string.IsNullOrEmpty(strArray[i]))
{
string str3 = strArray[i].Trim();
int length = str3.IndexOf("=");
if (length > 0)
{
string key = str3.Substring(0, length);
if (!hashtable.ContainsKey(key))
{
string str5 = str3.Substring(length + 1);
hashtable.Add(key, str5);
}
}
}
}
foreach (object obj2 in hashtable.Keys)
{
if (obj2.ToString().ToLower() == "path")
{
cookie.Path = hashtable[obj2].ToString();
}
else if (obj2.ToString().ToLower() == "expires")
{
DateTime time;
if (DateTime.TryParse(hashtable[obj2].ToString(), out time))
{
cookie.Expires = time;
}
}
else if (obj2.ToString().ToLower() == "domain")
{
if (hashtable[obj2].ToString().ToLower() == "koubei.com")
{
cookie.Domain = "." + hashtable[obj2].ToString();
}
else if (hashtable[obj2].ToString().ToLower() == "58.com")
{
cookie.Domain = "." + hashtable[obj2].ToString();
}
else
{
cookie.Domain = hashtable[obj2].ToString();
}
}
else if (obj2.ToString().ToLower() == "version")
{
cookie.Version = int.Parse(hashtable[obj2].ToString());
}
else if (!(obj2.ToString().ToLower() == "max-age"))
{
cookie.Expires = new DateTime(0x802, 1, 1);
cookie.Expired = false;
}
}
if (cookie.Name == "")
{
return null;
}
if (cookie.Domain == "")
{
cookie.Domain = defaultDomain;
}
return cookie;
} private Cookie FindCookie(string Url, string name)
{
if (Url.Substring(0, 5).ToLower() != "http:")
{
Url += "http://" + Url;
}
CookieCollection cookies = container.GetCookies(new Uri(Url));
if (cookies != null)
{
foreach (Cookie cookie in cookies)
{
if (cookie.Name == name)
{
return cookie;
}
}
}
return null;
}
} class HttpClient : WebClient
{
// Cookie 容器
private CookieContainer cookieContainer; /**/
/// <summary>
/// 创建一个新的 WebClient 实例。
/// </summary>
public HttpClient()
{
this.cookieContainer = new CookieContainer();
} /**/
/// <summary>
/// 创建一个新的 WebClient 实例。
/// </summary>
/// <param name="cookie">Cookie 容器</param>
public HttpClient(CookieContainer cookies)
{
this.cookieContainer = cookies;
} /**/
/// <summary>
/// Cookie 容器
/// </summary>
public CookieContainer Cookies
{
get { return this.cookieContainer; }
set { this.cookieContainer = value; }
} /**/
/// <summary>
/// 返回带有 Cookie 的 HttpWebRequest。
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
HttpWebRequest httpRequest = request as HttpWebRequest;
httpRequest.CookieContainer = cookieContainer;
}
return request;
}
}

有WebClient DownloadData,比较大的数据页面可以使用它下载,比Request请求快多了。

支持Cookie修改,域名重定向,富媒体表单,异步请求,事件完成。做个58同城登录分分钟的事情

HttpTooler tooler = new HttpTooler();
tooler.RequestCompleted = OnRequestCompleted;
tooler.GetHtml(RequestType.WebRequest, "www.58.com"); void OnRequestCompleted(object sender, string html)
{
//参数html 就是返回的结果
// do you want...
}

that's all enjoy...

自己封装的HttpRequest,个人觉的比较HttpHelper好用的更多相关文章

  1. Java原始封装常用HttpRequest

    1. package com.jsoft.testjavathread.test1; import java.io.BufferedReader; import java.io.IOException ...

  2. Django中HttpRequest和HttpResponse

    请求和响应对象 Django中通过使用请求和响应对象来传递系统的状态. 当请求一个页面的时候,Django就创建一个HttpRequest对象,它包含了关于请求的元数据对象,然后Django加载适当的 ...

  3. Django框架(五)-- 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传

    一.视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. ...

  4. Django框架之第四篇(视图层)--HttpRequest对象、HttpResponse对象、JsonResponse、CBV和FBV、文件上传

    视图层 一.视图函数 一个视图函数,简称视图,是一个简单的python函数,它接收web请求并且会返回web响应.响应可以是一张网页的html,一个重定向,或者是一张图片...任何东西都可以.无论是什 ...

  5. Django 之一些request封装的常用功能

    一些常用的request对象属性 介绍 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其 ...

  6. drf:restful概念,类继承关系,drf请求封装,drf请求流程,版本控制组件,认证组件(token),权限组件

    1.restful规范 resfful规范的概念最重要: 是一套规范,规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的就是,以前写增删改查的时候需要些四个视图寒素,rest ...

  7. Django框架(六)—— 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传

    目录 视图层 一.视图函数 二.视图层之HttpRequest对象 三.视图层之HttpResponse对象 四.视图层之JsonResponse对象 五.CBV和FBV 六.文件上传 视图层 一.视 ...

  8. drf(请求封装/认证/权限/节流)

    1.请求的封装 class HttpRequest(object): def __init__(self): pass @propery def GET(self): pass @propery de ...

  9. 十. Axios网络请求封装

    1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...

随机推荐

  1. Stm32 定时器 定时时间设置及PWM频率 占空比的设置总结

    一.定时器的时钟: 当SYSCLK等于72M,APB1等于36M APB2等于72M时,定时器的时钟为72M.注意图中这句话:如果APB1/APB2预分频器=1则频率不变,否则频率x2.如果此时,AP ...

  2. sql server 主从数据库同步 利用发布 订阅是实现

    发布订阅功能自SQL 2005之后就有了 配置方法可参考:http://blog.csdn.net/wanmdb/article/details/7515277 遇到问题: 1. 如果代理无法启动 , ...

  3. 火狐解决 OCSP 回应包含过期信息的问题_firefox吧_百度贴吧

    火狐解决 OCSP 回应包含过期信息的问题_firefox吧_百度贴吧 火狐解决 OCSP 回应包含过期信息的问题

  4. Jquery中常见问题

    最近也是在做项目的时候刚接触到jQuery.下面汇总一下遇到的几个问题 如何动态创建按钮 方式一: $("#a").html("<input type='butto ...

  5. 重写扫雷(基于jQuery) 新手 有不足的地方敬请谅解

    记得刚开始学习js的时候写过一次扫雷,一个下午的时间被计算搞死,整个头是晕乎. 入职后,蹭着空闲的时间随手写了一个扫雷. 直接上代码了 (function() { function module() ...

  6. Hitting the 2100 parameter limit (SQL Server) when using Contains()

    1down vote My solution (Guides -> List of Guid): List<tstTest> tsts = new List<tstTest&g ...

  7. [Angular 2] Using a Reducer to Change an Object's Property Inside an Array

    Reducers are also often used for changing a single property inside of other reducers. This lesson sh ...

  8. 一个int类型究竟占多少个字节

    一个int占多少个字节? 这个问题我们往往得到的答案是4. 可是int究竟占多少个字节,却跟你的机器环境有关. As you can see, the typical data type sizes ...

  9. SQL 使用Cursor(游标)遍历结果集

    使用Cursor(游标)可以在存储过程中遍历select 结果集,对其进行相关的操作. Cursor(游标)语法格式 DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段 ...

  10. javascript中的继承用法

    本文实例汇总了javascript关于继承的用法,希望本文所述对大家的javascript程序设计有所帮助.分享给大家供大家参考.具体如下:代码如下: /** * 实现子类继承父类,但不会产生多余的属 ...