自己封装的HttpRequest,个人觉的比较HttpHelper好用
新年开篇,忙归忙,还是要写点什么,不然越来越懒,分享我写的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好用的更多相关文章
- Java原始封装常用HttpRequest
1. package com.jsoft.testjavathread.test1; import java.io.BufferedReader; import java.io.IOException ...
- Django中HttpRequest和HttpResponse
请求和响应对象 Django中通过使用请求和响应对象来传递系统的状态. 当请求一个页面的时候,Django就创建一个HttpRequest对象,它包含了关于请求的元数据对象,然后Django加载适当的 ...
- Django框架(五)-- 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传
一.视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. ...
- Django框架之第四篇(视图层)--HttpRequest对象、HttpResponse对象、JsonResponse、CBV和FBV、文件上传
视图层 一.视图函数 一个视图函数,简称视图,是一个简单的python函数,它接收web请求并且会返回web响应.响应可以是一张网页的html,一个重定向,或者是一张图片...任何东西都可以.无论是什 ...
- Django 之一些request封装的常用功能
一些常用的request对象属性 介绍 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其 ...
- drf:restful概念,类继承关系,drf请求封装,drf请求流程,版本控制组件,认证组件(token),权限组件
1.restful规范 resfful规范的概念最重要: 是一套规范,规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的就是,以前写增删改查的时候需要些四个视图寒素,rest ...
- Django框架(六)—— 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传
目录 视图层 一.视图函数 二.视图层之HttpRequest对象 三.视图层之HttpResponse对象 四.视图层之JsonResponse对象 五.CBV和FBV 六.文件上传 视图层 一.视 ...
- drf(请求封装/认证/权限/节流)
1.请求的封装 class HttpRequest(object): def __init__(self): pass @propery def GET(self): pass @propery de ...
- 十. Axios网络请求封装
1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...
随机推荐
- Java---多线程之死锁
★ 死锁的两种情况: 简单的说下单块cpu运行多线程的情况: 大家可能平时玩电脑,可以同时挂QQ啊,玩游戏啊,打开文本啊,等等.这里,我们假设是单块cpu.也就是俗称的单核cpu. 大家可能会觉得这些 ...
- LoadRunner基础入门教程
原文地址:http://jingyan.baidu.com/article/215817f7dedfb01eda1423df.html 从LoadRunner英语字面上进行理解就是负载跑步者,为什么这 ...
- socket编程里的read和recv函数【转载】
本文转载自:http://blog.163.com/like12@126/blog/static/63023403201291983117551/ 1.read 与 recv 区别 read 原则: ...
- Java学习日记-2.4 浮点数
http://wenku.baidu.com/view/e68b2ed5312b3169a451a4e2.html
- 《鸟哥Linux私房菜基础学习篇》命令索引
在学习的过程,由于很多命令平时都用不着,因此做这个索引方便需要时查找.这包括了前两部分.主要是按页码顺序. P118 date:显示日期与时间 cal:显示日历 bc:计算器 P121 [Tab]:命 ...
- MVC路由机制
按照传统,在很多Web框架中(如经典的ASP.JSP.PHP.ASP.NET等之类的框架),URL代表的是磁盘上的物理文件.例如,当看到请求http://example.com/albums/li ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c
遗传算法中的交叉操作是 对NSGA-II 源码分析的 最后一部分, 这一部分也是我 从读该算法源代码和看该算法论文理解偏差最大的 函数模块. 这里,首先提一下,遗传算法的 交叉操作.变异操作都 ...
- jenkins服务器安装
http://www.360doc.com/content/13/0412/09/10504424_277718090.shtml
- HTML5 Canvas渐进填充与透明
详细解释HTML5 Canvas中渐进填充的参数设置与使用,Canvas中透明度的设置与使 用,结合渐进填充与透明度支持,实现图像的Mask效果. 一:渐进填充(Gradient Fill) Canv ...
- OleDb 读取 excel
//定义变量 _Application ExcelApp; Workbooks wbsMyBooks; _Workbook wbMyBook; Worksheets wssMysheets; _Wor ...