新年开篇,忙归忙,还是要写点什么,不然越来越懒,分享我写的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. Flask+Mysql搭建网站之其他笔记

    写在前面 之前用过python的另外一个框架,Django.感觉Django比Flask的资料要多.做这个网站的时候,遇到一些棘手的问题,怎么百度也就只能找到翻来覆去的官方文档以及miguelgrin ...

  2. SQL Server优化之SQL语句优化

    一切都是为了性能,一切都是为了业务 一.查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_conditi ...

  3. Linq 语法举例

    1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...

  4. XXE篇-本着就了解安全本质的想法,尽可能的用通俗易懂的语言去解释安全漏洞问题

    0x01 Brief Description XXE(XML External Entity) XML外部实体攻击也是常见的web漏洞之一,在学习这个漏洞之前有必要了解一下xml,可以参考w3c的基本 ...

  5. web端及时通讯原理

    前言 有关IM(InstantMessaging)聊天应用(如:微信,QQ).消息推送技术(如:现今移动端APP标配的消息推送模块)等即时通讯应用场景下,大多数都是桌面应用程序或者native应用较为 ...

  6. oracle管道输出

    通常我们会在oracle中用dbms_output输出调试信息,但dbms_output只能在调用过程完成才返回结果,不能实时输出的.这意味着通常我们经常要等几分钟或更长的时间才能看到调试信息,那怎么 ...

  7. ubuntu安装mysql的步骤和配置总结

    因为经常要在ubuntu linux的环境下做一些开发工作.很多时候也牵涉到mysql相关的开发工作.于是就把整个过程做了一个整理,以方便以后再次安装配置的时候参考,也希望能够让新手少走点弯路. 其实 ...

  8. C primer plus 读书笔记第五章

    本章的标题是运算符,表达式和语句.主要研究如何处理数据. 示例代码展示了一个使用简单的while循环的代码,难度不大. 下面简单介绍本章的基本概念. 1.基本运算符. 基本运算符有:赋值运算符(C语言 ...

  9. 4G时代来临,运营商为谁搭台献唱?

        4G时代已然来临.对用户而言,4G意味着更快的传输速度,以及更优质的移动网络体验. 只是对运营商而言.怎样部署4G却成了一大难题.众所周知,在全球范围内,4G LTE成为眼下最率先的移动宽带解 ...

  10. hdu1074 Doing Homework(状态压缩DP Y=Y)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...