C# 一个特别不错的http请求类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions; namespace aaa
{
public class ResponseModel
{
private WebHeaderCollection header;
/// <summary>
/// 返回的头部信息集合
/// </summary>
public WebHeaderCollection Header
{
get { return header; }
set { header = value; }
}
private string html;
/// <summary>
/// 返回的文本内容
/// </summary>
public string Html
{
get { return html; }
set { html = value; }
}
private Stream stream;
/// <summary>
/// 返回的流内容
/// </summary>
public Stream Stream
{
get { return stream; }
set { stream = value; }
} }
public class HttpHelper
{
private string accept = "application/json,text/javascrip{过滤}t,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
private System.Net.CookieContainer cc = new System.Net.CookieContainer();
private string contentType = "application/x-www-form-urlencoded"; private int timeOut = ;
public NameValueCollection Heads = new NameValueCollection();
private bool AllowAutoRedirect = false;
bool needReset = false;
private System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8"); public IWebProxy Proxy;
private string[] userAgents = new string[] { "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0" }; private string userAgent
{
get
{
return this.userAgents[new Random().Next(, this.userAgents.Length)];
}
} /// <summary>
/// 设置下一次请求为自动重定向
/// </summary>
/// <param name="value"></param>
public void SetAllowAutoRedirectOneTime(bool value)
{
AllowAutoRedirect = value;
needReset = true;
} /// <summary>
/// 网页访问
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="isPost">是否Post</param>
/// <param name="postData">Post数据内容</param>
/// <param name="retType">返回类型0为文本,1为Stream</param>
/// <param name="cookieContainer">cookie</param>
/// <param name="refurl">Referer</param>
/// <param name="_contentType">contentType</param>
/// <param name="headers">请求头</param>
/// <returns></returns>
public ResponseModel HttpVisit(string url, bool isPost = false, string postData = null, int retType = , System.Net.CookieContainer cookieContainer = null, string refurl = null, string _contentType = null, NameValueCollection headers = null)
{
if (cookieContainer == null)
{
cookieContainer = this.cc;
} if (!isPost)
{
return GetHtml(url, refurl, cookieContainer, _contentType, headers, retType);
} ResponseModel model = new ResponseModel(); ServicePointManager.Expect100Continue = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create(url);
if (this.Proxy != null) request.Proxy = this.Proxy;
request.CookieContainer = cookieContainer;
request.Timeout = timeOut;
if (string.IsNullOrEmpty(_contentType))
{
request.ContentType = this.contentType;
}
else
{
request.ContentType = _contentType;
} if (string.IsNullOrEmpty(refurl))
{
request.Referer = url;
}
else
{
request.Referer = refurl;
} request.AllowAutoRedirect = AllowAutoRedirect;
request.Accept = this.accept;
request.UserAgent = this.userAgent; if (headers != null)
{
request.Headers.Add(Heads);
request.Headers.Add(headers);
}
else
{
request.Headers.Add(Heads);
} request.Method = isPost ? "POST" : "GET";
request.ContentLength = bytes.Length; Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, , bytes.Length);
requestStream.Close(); if (retType == )
{
response = (HttpWebResponse)request.GetResponse(); model.Header = response.Headers; Stream responseStream = response.GetResponseStream(); if (response.Cookies.Count > )
{
this.cc.Add(response.Cookies);
} model.Stream = responseStream;
return model; } string str = string.Empty;
response = (HttpWebResponse)request.GetResponse(); model.Header = response.Headers; string encoding = "utf-8"; if (!string.IsNullOrEmpty(response.CharacterSet))
{
encoding = response.CharacterSet.ToLower();
}
else
{
encoding = this.encoding.HeaderName;
} if (response.ContentEncoding.ToLower().Contains("gzip"))
{ using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{ str = reader.ReadToEnd();
}
}
}
else if (response.ContentEncoding.ToLower().Contains("deflate"))
{
using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{ str = reader.ReadToEnd();
} }
}
else
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{ str = reader.ReadToEnd();
}
}
} request.Abort();
response.Close();
request.Abort();
if (response.Cookies.Count > )
{
this.cc.Add(response.Cookies);
}
model.Html = str;
return model;
}
catch (Exception ex)
{
if (request != null) request.Abort();
if (response != null)
{
response.Close(); return new ResponseModel() { Html = ex.Message, Header = response.Headers };
}
return new ResponseModel() { Html = ex.Message };
}
finally
{
if (needReset)
{
AllowAutoRedirect = false;
needReset = false;
}
}
}
/// <summary>
/// 清理string类型Cookie.剔除无用项返回结果为null时遇见错误.
/// </summary>
/// <param name="Cookies"></param>
/// <returns></returns>
public CookieCollection ClearCookie(string Cookies)
{
try
{
CookieCollection cookies = new CookieCollection();
string rStr = string.Empty;
Cookies = Cookies.Replace(";", "; ");
Regex r = new Regex("(?<=,)(?<cookie>[^ ]+=(?!deleted;)[^;]+);");
MatchCollection ms = r.Matches("," + Cookies);
foreach (Match m in ms)
{
string[] cookie = m.Groups["cookie"].Value.Split('='); if (cookie.Length > )
cookies.Add(new Cookie(cookie[], cookie[])); }
return cookies;
}
catch
{
return new CookieCollection();
}
} private ResponseModel GetHtml(string url, string refurl = null, System.Net.CookieContainer cookieContainer = null, string _contentType = "", NameValueCollection headers = null, int retType = )
{
if (cookieContainer == null)
{
cookieContainer = this.cc;
} ResponseModel model = new ResponseModel(); ServicePointManager.Expect100Continue = true; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (this.Proxy != null) request.Proxy = this.Proxy;
request.CookieContainer = cookieContainer;
request.Timeout = timeOut;
if (string.IsNullOrEmpty(_contentType))
{
request.ContentType = this.contentType;
}
else
{
request.ContentType = _contentType;
} if (string.IsNullOrEmpty(refurl))
{
request.Referer = url;
}
else
{
request.Referer = refurl;
} request.AllowAutoRedirect = AllowAutoRedirect;
request.Accept = this.accept;
request.UserAgent = this.userAgent; if (headers != null)
{
request.Headers.Add(Heads);
request.Headers.Add(headers);
}
else
{
request.Headers.Add(Heads);
} request.Method = "GET"; if (retType == )
{
response = (HttpWebResponse)request.GetResponse(); model.Header = response.Headers; Stream responseStream = response.GetResponseStream(); if (response.Cookies.Count > )
{
this.cc.Add(response.Cookies);
} model.Stream = responseStream; return model; } string str = string.Empty;
response = (HttpWebResponse)request.GetResponse(); model.Header = response.Headers; string encoding = "utf-8"; if (!string.IsNullOrEmpty(response.CharacterSet))
{
encoding = response.CharacterSet.ToLower();
}
else
{
encoding = this.encoding.HeaderName;
} if (response.ContentEncoding.ToLower().Contains("gzip"))
{ using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{ str = reader.ReadToEnd();
}
}
}
else if (response.ContentEncoding.ToLower().Contains("deflate"))
{
using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{ str = reader.ReadToEnd();
} }
}
else
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{ str = reader.ReadToEnd();
}
}
} if (response.Cookies.Count > )
{
cookieContainer.Add(response.Cookies);
} request.Abort();
response.Close(); model.Html = str;
return model;
}
catch (Exception ex)
{
if (request != null) request.Abort();
if (response != null)
{
response.Close(); return new ResponseModel() { Html = ex.Message, Header = response.Headers };
}
return new ResponseModel() { Html = ex.Message };
}
finally
{
if (needReset)
{
AllowAutoRedirect = false;
needReset = false;
}
}
} private bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
//直接通过HTTPS的证书请求
return true;
} public Stream GetStream(string url)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (this.Proxy != null) request.Proxy = this.Proxy;
request.CookieContainer = this.cc;
request.ContentType = this.contentType;
// request.ServicePoint.ConnectionLimit = this.maxTry;
request.Timeout = 0x1388;
request.Referer = url;
request.Accept = this.accept;
request.UserAgent = this.userAgent;
request.Method = "GET";
response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream();
// this.currentTry--;
if (response.Cookies.Count > )
{
this.cc.Add(response.Cookies);
}
return responseStream;
}
catch (Exception ex)
{
// if (this.currentTry <= this.maxTry) this.GetHtml(url, cookieContainer);
// this.currentTry--;
if (request != null) request.Abort();
if (response != null) response.Close();
return null;
}
} #region String与CookieContainer互转
/// <summary>
/// 将String转CookieContainer
/// </summary>
/// <param name="url"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public CookieContainer StringToCookie(string url, string cookie)
{
string[] arrCookie = cookie.Split(';');
CookieContainer cookie_container = new CookieContainer(); //加载Cookie
foreach (string sCookie in arrCookie)
{
if (sCookie.IndexOf("expires") > )
continue;
cookie_container.SetCookies(new Uri(url), sCookie);
}
return cookie_container;
} /// <summary>
/// 将CookieContainer转换为string类型
/// </summary>
/// <param name="cc"></param>
/// <returns></returns>
public string GetCookieString()
{
System.Collections.Generic.List<Cookie> lstCookies = new System.Collections.Generic.List<Cookie>();
Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
StringBuilder sb = new StringBuilder();
foreach (object pathList in table.Values)
{
SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
| System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
foreach (CookieCollection colCookies in lstCookieCol.Values)
foreach (Cookie c in colCookies)
{
sb.Append(c.Name).Append("=").Append(c.Value).Append(";");
}
}
return sb.ToString();
}
#endregion }
}
C# 一个特别不错的http请求类的更多相关文章
- 一个比较强大的HTTP请求类,支持文本参数和文件参数。
一个 http 请求类 ,支持文件上传,从淘宝 top sdk 里面扣出来的,蛮好用的,做个记录而已. 调用代码: Dictionary<string, string> textParas ...
- 一个特别不错的jQuery快捷键插件:js-hotkeys
这其实不是什么新技术,这个插件在很早前就已经发布了,之前有项目用到,所以分享出来添加方式的例子 jQuery.hotkeys.add('esc',function (){ //执行函数 }); jQu ...
- 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)
目录 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载) Http协议简述 HttpRequest类设计 请求部分 接收部分 关于上传和下载 Cpp实现 关于源码中的Lo ...
- block传值以及利用block封装一个网络请求类
1.block在俩个UIViewController间传值 近期刚学了几招block 的高级使用方法,事实上就是利用block语法在俩个UIViewController之间传值,在这里分享给刚開始学习 ...
- 用CIL写程序:定义一个叫“慕容小匹夫”的类
前文回顾: <用CIL写程序:你好,沃尔德> <用CIL写程序:写个函数做加法> 前言: 今天是乙未羊年的第一天,小匹夫先在这里给各位看官拜个年了.不知道各位看官是否和匹夫一样 ...
- 【PHPsocket编程专题(实战篇③)】构建基于socket的HTTP请求类
该代码是两年前写的,现在看起来有点渣了,仅仅是提供一个思路,现在做一些Api开发的时候官方会有一些SDK,这些SDK其实原理都是通过socket来通讯的,其实我个人主张用curl更方便,当然前提是你的 ...
- 发送一个简单的http get 请求并且响应
问题 如何发送一个简单的HTTP GET请求并且取回相应的HTTP响应. 设计 创建一个WebClient类的实例,然后使用它的DownloadData()方法. 方案 string uri = &q ...
- WinSock IOCP 模型总结(附一个带缓存池的IOCP类)
前言 本文配套代码:https://github.com/TTGuoying/IOCPServer 由于篇幅原因,本文假设你已经熟悉了利用Socket进行TCP/IP编程的基本原理,并且也熟练的掌握了 ...
- Servlet(五):一个Servlet处理多个请求
一.为什么要使用一个Servlet来处理多个请求? 当浏览器发送了一次请求到服务器时,servlet容器会根据请求的url-pattern找到对应的Servlet类,执行对应的doPost或doGet ...
随机推荐
- [国家集训队]整数的lqp拆分
我们的目标是求$\sum\prod_{i=1}^m F_{a_i}$ 设$f(i) = \sum\prod_{j=1}^i F_{a_j}$那么$f(i - 1) = \sum\prod_{j=1}^ ...
- SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。
SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...
- 覆盖的面积 HDU - 1255 (扫描线, 面积交)
求n个矩阵面积相交的部分,和求面积并一样,不过这里需要开两个数组保存覆盖一次和覆盖两次以上的次数的部分,还是模板,主要注意点就是pushup部分,如果我已经被两次覆盖,那我的两个数组在这个root点的 ...
- 洛谷4451 整数的lqp拆分(生成函数)
比较水的一题.居然是一道没看题解就会做的黑题…… 题目链接:洛谷 题目大意:定义一个长度为 $m$ 的正整数序列 $a$ 的价值为 $\prod f_{a_i}$.($f$ 是斐波那契数)对于每一个 ...
- CANOE入门(三)
最好的学习方式是什么?模仿.有人会问,那不是山寨么?但是我认为,那是模仿的初级阶段,当把别人最好的设计已经融化到自己的血液里,变成自己的东西,而灵活运用的时候,才是真正高级阶段.正所谓画虎画皮难画骨. ...
- centos7下mysql半同步复制原理安装测试详解
原理简介: 在MySQL5.5之前,MySQL的复制其实都是异步复制(见下图),主库和从库的数据之间存在一定的延迟,这样存在一个隐患:当在主库上写入一个事务并提交成功,而从库尚未得到主库推送的BinL ...
- 工厂方法模式(Factory Method)和抽象工厂模式(Abstact Factory)
分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的.工厂模式在<Java 与模式>中分为三类:1)简单工厂模式(Simple Facto ...
- Windows下VMware14黑屏
解决方法 以管理员身份运行命令提示符,执行netsh winsock reset
- 把axios封装为vue插件使用
前言 自从Vue2.0推荐大家使用 axios 开始,axios 被越来越多的人所了解.使用axios发起一个请求对大家来说是比较简单的事情,但是axios没有进行封装复用,项目越来越大,引起的代码冗 ...
- 第三十四篇-Palette(调色板)的使用
由于屏幕录制图片转换关系,不甚清晰,还是附上效果图 可以看出,上面文字和背景颜色确实会根据图片的变化而变化. 里面有3个组件,toolbar,textview,imageview,其中textview ...