我正在创建自己的HTTPWebRequest类,并且我正在尝试优化读取缓冲区和发出请求所需的时间,到目前为止它需要 800-1000ms 对于google.com的每个请求
C ++中的相同代码需要〜 200ms 任何建议我如何更好地优化它?
public class OWebReq
{
public string LastError;
public bool LastOK = false;
public OWebReq()
{
}
public static void Init()
{
ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = 9999;
WebRequest.DefaultWebProxy = (WebProxy)null;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
}
public string GET(string url, WebProxy proxy = null)
{
LastOK = false;
var req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
req.Proxy = proxy;
req.KeepAlive = false;
req.Timeout = 5000;
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
try
{
using(var response=(HttpWebResponse)req.GetResponse())
{
var responseStream = response.GetResponseStream();
if(responseStream!=null)
{
using (var sr = new StreamReader(responseStream))
{
return sr.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
LastError = ex.Message;
return null;
}
return null;
}
}
执行代码直到var response=(HttpWebResponse)req.GetResponse()
需要〜 350ms
答案 0 :(得分:0)
尝试将ServicePointManager.Expect100Continue
或req.ServicePoint.Expect100Continue
设为 false 。
基本要点是,请求最初只发送标头并等待服务器响应代码100(继续)。这个上限为350毫秒。收到响应后,将发送请求正文。这样,当服务器无论如何都要拒绝请求(需要重定向或身份验证,或者标题中出现错误等)时,您不会在线路上发出非常大的请求(具有许多MB的主体)。 / p>
这link有一个非常详细的解释。