如何在 C# 中执行快速的 Web 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1119347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to perform a fast web request in C#
提问by Morten Christiansen
I have a HTTP based API which I potentially need to call many times. The problem is that I can't get the request to take less than about 20 seconds, though the same request made through a browser is near instantaneous. The following code illustrates how I have implemented it so far.
我有一个基于 HTTP 的 API,我可能需要多次调用它。问题是我无法让请求花费不到大约 20 秒,尽管通过浏览器发出的相同请求几乎是瞬时的。以下代码说明了到目前为止我是如何实现它的。
WebRequest r = HttpWebRequest.Create("https://example.com/http/command?param=blabla");
var response = r.GetResponse();
One solution would be to make an asynchronous request but I would like to know why it takes so long and if I can avoid it. I have also tried using the WebClient class but I suspect it uses a WebRequest internally.
一种解决方案是发出异步请求,但我想知道为什么需要这么长时间以及是否可以避免它。我也尝试过使用 WebClient 类,但我怀疑它在内部使用了 WebRequest。
Update:
更新:
Running the following code took about 40 seconds in Release Mode (measured with Stopwatch):
在发布模式下运行以下代码大约需要 40 秒(用秒表测量):
WebRequest g = HttpWebRequest.Create("http://www.google.com");
var response = g.GetResponse();
I'm working at a university where there might be different things in the network configuration affecting the performance, but the direct use of the browser illustrates that it should be near instant.
我在一所大学工作,那里的网络配置可能会影响性能,但直接使用浏览器说明它应该是近乎即时的。
Update 2:
更新 2:
I uploaded the code to a remote machine and it worked fine so the conclusion must be that the .NET code does something extra compared to the browser or it has problems resolving the address through the university network (proxy issues or something?!).
我将代码上传到远程机器,它运行良好,所以结论一定是 .NET 代码与浏览器相比做了一些额外的事情,或者它在通过大学网络解析地址时遇到问题(代理问题或其他问题?!)。
回答by Philip Rieck
Does your site have an invalid SSL cert? Try adding this
您的网站是否有无效的 SSL 证书?尝试添加这个
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AlwaysAccept);
//... somewhere AlwaysAccept is defined as:
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
public bool AlwaysAccept(object sender, X509Certificate certification, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
回答by James Roland
This problem is similar to another post on StackOverflow: Stackoverflow-2519655(HttpWebrequest is extremely slow)
这个问题类似于 StackOverflow 上的另一篇文章: Stackoverflow-2519655(HttpWebrequest 非常慢)
Most of the time the problem is the Proxy server property. You should set this property to null, otherwise the object will attempt to search for an appropriate proxy server to use before going directly to the source. Note: this property is turn on by default, so you have to explicitly tell the object not to perform this proxy search.
大多数情况下,问题是代理服务器属性。您应该将此属性设置为 null,否则对象将在直接转到源之前尝试搜索要使用的适当代理服务器。注意:这个属性默认是开启的,所以你必须明确告诉对象不要执行这个代理搜索。
request.Proxy = null;
using (var response = (HttpWebResponse)request.GetResponse())
{
}
回答by Dave Amphlett
I was having the 30 second delay on 'first' attempt - JamesR's reference to the other post mentioning setting proxy to null solved it instantly!
我在“第一次”尝试时延迟了 30 秒 - JamesR 引用了另一篇提到将代理设置为 null 的帖子,立即解决了这个问题!
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_site.url);
request.Proxy = null; // <-- this is the good stuff
...
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
回答by Nicolas78
You don't close your Request. As soon as you hit the number of allowed connections, you have to wait for the earlier ones to time out. Try
你没有关闭你的请求。一旦达到允许的连接数,就必须等待较早的连接超时。尝试
using (var response = g.GetResponse())
{
// do stuff with your response
}