先设置4个:

[csharp]
webrequest.ServicePoint.Expect100Continue = false;
//是否使用 Nagle 不使用 提高效率
webrequest.ServicePoint.UseNagleAlgorithm = false;
//最大连接数
webrequest.ServicePoint.ConnectionLimit = 65500;
//数据是否缓冲 false 提高效率
webrequest.AllowWriteStreamBuffering = false; 代理的使用:我用的代理都是不要账号和密码的,因为要不是免费的,要是是我们自己的,对来访IP 有限制 [csharp]
if (proxy!=null)
{
model.proxy = proxy;
WebProxy myProxy = new WebProxy(proxy.ProxyInfo, false);
myProxy.Credentials = new NetworkCredential("", "");
webrequest.Proxy = myProxy;
}
else
{
webrequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();
} webrequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); 带上gzip,如果对方支持gzip,流量上可以节省五分之四,原来250KB,用gzip压缩有 大概在50KB,减少带宽压力,增加速度 值得注意的是,如果是gzip,返回值必须要gzip解压的。 [csharp]
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{ model.html = reader.ReadToEnd();
}
}
} 还有一点:gzip解压的时候回耗费CPU,所以这个需要注意下 附完成方法 [csharp]
/// <summary>
/// Get 方式 获取数据
/// </summary>
/// <param name="webUrl"></param>
/// <returns></returns>
public Model.WebRequestReturnModel WebRequestGetHtmlByGet(string webUrl, CookieContainer cookieContainer, Encoding encoding, string refer,Model.Proxy proxy)
{
Model.WebRequestReturnModel model = new Model.WebRequestReturnModel();
HttpWebRequest webrequest = null;
try
{
webrequest = WebRequest.Create(webUrl) as HttpWebRequest; webrequest.ServicePoint.Expect100Continue = false;
webrequest.ServicePoint.UseNagleAlgorithm = false;
webrequest.ServicePoint.ConnectionLimit = 65500;
webrequest.AllowWriteStreamBuffering = false;
if (proxy!=null)
{
model.proxy = proxy;
WebProxy myProxy = new WebProxy(proxy.ProxyInfo, false);
myProxy.Credentials = new NetworkCredential("", "");
webrequest.Proxy = myProxy;
}
else
{
webrequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();
}
//设置其他的herader
webrequest =WebRequestSetHeaders(webrequest);
if (!String.IsNullOrEmpty(refer))
{
webrequest.Referer = refer;
}
else
{
webrequest.Referer = webUrl;
}
webrequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
webrequest.CookieContainer = cookieContainer;
webrequest.Timeout = 5000;
webrequest.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
webrequest.Accept = "*/*";
webrequest.KeepAlive = true;
webrequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); using (HttpWebResponse response = (HttpWebResponse)webrequest.GetResponse())
{ if (response.ContentEncoding.ToLower().Contains("gzip"))
{
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{ model.html = 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.UTF8))
{ model.html = reader.ReadToEnd();
} }
}
else
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{ model.html = reader.ReadToEnd();
}
}
}
}
}
catch (Exception ex)
{
model.exception = ex;
model.html = string.Empty; }
model.cookieContainer = cookieContainer;
return model;
}

  

HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题的更多相关文章

  1. nginx证书制作以及配置https并设置访问http自动跳转https(反向代理转发jboss)

    nginx证书制作以及配置https并设置访问http自动跳转https 默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖 ...

  2. 企业IT管理员IE11升级指南【15】—— 代理自动配置脚本

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  3. 利用mock提高效率

    利用mock提高效率 谈到mock,就不得不讲前后端分离.理想情况下前后端不分离,由全栈的人以product和infrastructure的维度进行开发,效率是最高的.近些年来业务的复杂度越来越高,真 ...

  4. 分享两个提高效率的AndroidStudio小技巧

    这次分享两个 Android Studio 的小技巧,能够有效提高效率和减少犯错,尤其是在团队协作开发中. Getter 模板修改--自动处理 null 判断 格式化代码自动整理方法位置--广度 or ...

  5. grunt配置太复杂?发布一个前端构建工具,简单高效,自动跳过未更新的文件

    做前端项目,如果没有一个自动化构建工具,手动处理那简直就是坑爹O(∩_∩)O.于是上网了解了下,grunt用的人不少,功能也挺强大.看了一下grunt的配置(包括gulp),感觉稍显复杂.当时项目结构 ...

  6. 提高效率的Matlab使用方式

    1.花一点时间学习一些提高效率的技巧永远是值得的: 2.总结和记录永远是必要的. Command窗口: Editor窗口: 1.Tab自动补全

  7. paip.提高效率---集合的存取括号方式 uapi java python php js 的实现比较

    paip.提高效率---集合的存取括号方式 uapi java python php js 的实现比较 ##java ----------- 在JDK1.7中,摒弃了Java集合接口的实现类,如:Ar ...

  8. http自动跳转https小记

    因近期跌代更新ios/android后,由于担心ios https从17年起限制的问题,故目前我们将http更改为https,但既然支持https,故想将服务器直接更新为https,将原http请求自 ...

  9. nginx二级域名配置自动跳转到一级域名

    nginx二级域名配置自动跳转到一级域名 rewrite配置内容: if ($http_host !~ "^www.aaa.com$") { rewrite ^(.*) http: ...

随机推荐

  1. iOS开发中常用到的加密方式

    1 base64 1.1 简介 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24位数据,再把这24位数据 ...

  2. HDU1016(bfs)

    import java.util.Scanner;public class Main1016 { public static void main(String[] args) { Scanner ci ...

  3. jAVA HDU1001题

    import java.util.Scanner;public class Main { public static void main(String args[]) { Scanner cin=ne ...

  4. centos 下安装ati显卡驱动方法

    1)到ati的官网(http://support.amd.com/us/gpudownload/Pages/index.aspx)下载相应的驱动,一定要注意 radeon系列和mobility rad ...

  5. html代码实现自动滚动,鼠标滑过时停止滚动

    <marquee style="width: 1200px;height:200px;margin:0px auto" onmouseout="this.start ...

  6. webform 转 MVC 飞一般的感觉

    前言: 浅谈webform与mvc,让开发变得更加简单,这里主要通过比较webform与mvc的开发方式,以下全属个人看法,不完善的地方可以留言补充. 正文: 废话不多说,直接说工作中经常用到的地方 ...

  7. Asp.Net 如何获取所有控件&如何获取指定类型的所有控件

    一. Asp.Net Page页面中访问所有控件的属性为: Page.Controls 控件的结构是树结构. 二.获取指定类型所有控件实例: 1.递归方法定义: private void GetCon ...

  8. 编码、解码Html代码

    引用 Base64-80.js 文件 做网页的时候有时候需要把富文本框的html代码保存到数据库,那么就需要编码后保存到数据库.浏览器端或后台再解码作绑定 *编码:encode64(str) *解码: ...

  9. 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

  10. 转载---SQL Server XML基础学习之<5>--XQuery(query)

    本章写一些SQL Server XML的一些XQuery基础语法,主要讲的query查询语法 T-SQL 支持用于查询 XML 数据类型的 XQuery 语言的子集. XQuery 基于现有的 XPa ...