HttpWebRequest的非常慢! [英] HttpWebRequest is extremely slow!

查看:840
本文介绍了HttpWebRequest的非常慢!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是开源库连接到我的网络服务器。我担心的是网络服务器正想非常慢,然后我试着做在Ruby中一个简单的测试,我得到了这些结果。

I am using an open source library to connect to my webserver. I was concerned that the webserver was going extremely slow and then I tried doing a simple test in Ruby and I got these results

Ruby程序:2.11秒10 HTTP
  可获得

Ruby program: 2.11seconds for 10 HTTP GETs

Ruby程序:18.13秒100 HTTP
  可获得

Ruby program: 18.13seconds for 100 HTTP GETs

C#库:20.81秒10 HTTP
  可获得

C# library: 20.81seconds for 10 HTTP GETs

C#库:10036847.46秒HTTP
  可获得

C# library: 36847.46seconds for 100 HTTP GETs

我已经成型,发现问题是这样的功能:

I have profiled and found the problem to be this function:

private HttpWebResponse GetRawResponse(HttpWebRequest request) {
  HttpWebResponse raw = null;
  try {
    raw = (HttpWebResponse)request.GetResponse(); //This line!
  }
  catch (WebException ex) {
    if (ex.Response is HttpWebResponse) {
      raw = ex.Response as HttpWebResponse;
    }
  }
  return raw;
}

标记行是接替1秒本身来完成,而Ruby程序使得1请求花费0.3秒。我也做所有的127.0.0.1这些测试的,因此网络带宽不是问题。

The marked line is takes over 1 second to complete by itself while the ruby program making 1 request takes .3 seconds. I am also doing all of these tests on 127.0.0.1, so network bandwidth is not an issue.

什么引起这个巨大的减缓?

更新

查看更改的基准测试结果。其实,我有10 GET和没有100的测试,我的结果进行更新。

Check out the changed benchmark results. I actually tested with 10 GETs and not 100, I updated the results.

推荐答案

我发现是缓慢的Web请求的罪魁祸首是代理性质。如果你调用GetResponse的方法之前将此属性设置为null查询将跳过代理自动步:

What I have found to be the main culprit with slow web requests is the proxy property. If you set this property to null before you call the GetResponse method the query will skip the proxy autodetect step:

request.Proxy = null;
using (var response = (HttpWebResponse)request.GetResponse())
{
}

代理自动检测被占用为7秒返回响应之前进行查询。这是一个有点讨厌这个属性默认为HttpWebRequest对象设置。

The proxy autodetect was taking up to 7 seconds to query before returning the response. It is a little annoying that this property is set on by default for the HttpWebRequest object.

这篇关于HttpWebRequest的非常慢!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆