先設置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;
}