在 C# 中保持 http 连接处于活动状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/749030/
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
Keep a http connection alive in C#?
提问by
How do i keep a connection alive in C#? I'm not doing it right. Am i suppose to create an HttpWebRequest obj and use it to go to any URLs i need? i dont see a way to visit a url other then the HttpWebRequest.Create static method.
如何在 C# 中保持连接活动?我做得不对。我是否想创建一个 HttpWebRequest obj 并使用它来访问我需要的任何 URL?除了 HttpWebRequest.Create 静态方法之外,我没有看到访问 url 的方法。
How do i create a connection, keep it alive, browse multiple pages/media on the page and support proxys? (i hear proxy are easy and support is almost standard?) -edit- good answers. How do i request a 2nd url?
如何创建连接、保持连接、浏览页面上的多个页面/媒体并支持代理?(我听说代理很容易,支持几乎是标准的?)-编辑-很好的答案。我如何请求第二个网址?
{
HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create("http://google.com");
WebRequestObject.KeepAlive = true;
//do stuff
WebRequestObject.Something("http://www.google.com/intl/en_ALL/images/logo.gif");
}
回答by Lucero
You can set the HttpWebRequest.KeepAliveproperty to true.
您可以将HttpWebRequest.KeepAlive属性设置为 true。
For the Proxies, there is a property also: HttpWebRequest.Proxy
对于代理,还有一个属性:HttpWebRequest.Proxy
回答by petr k.
Have you tried the HttpWebRequest.KeepAliveproperty? It sets the appropriate Keep-Alive HTTP header and does persist the connections. (Of course this must also be supported and enabled by the remote web server).
您是否尝试过HttpWebRequest.KeepAlive属性?它设置适当的 Keep-Alive HTTP 标头并保持连接。(当然,这也必须得到远程 Web 服务器的支持和启用)。
The HttpWebRequest.KeepAlivedocumentation on MSDN states that it is set to trueby default for HTTP1.1 connections, so I suspect the server you're trying to contact does not allow connection persistence.
MSDN 上的HttpWebRequest.KeepAlive文档指出HTTP1.1 连接默认设置为true,因此我怀疑您尝试联系的服务器不允许连接持久性。
Proxy is used automatically and its settings are taken from your system (read Internet Explorer) settings. It is also possible to override the proxy settings via HttpWebRequest.Proxyproperty or by tweaking the application configuration file (see http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx).
代理会自动使用,其设置取自您的系统(阅读 Internet Explorer)设置。也可以通过HttpWebRequest.Proxy属性或通过调整应用程序配置文件来覆盖代理设置(请参阅http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx)。
回答by dr. evil
Set HttpWebRequest.KeepAliveproperty True .NET will take care of the rest. It's just database connection pool. Works transparently.
设置HttpWebRequest.KeepAlive属性 True .NET 将负责其余的工作。它只是数据库连接池。透明地工作。
You can create new connection freely .NET will figure it out that you are connecting an already connected server and will use it. Also depends on your Net.ServicePointManager.DefaultConnectionLimit
number it will establish new connections (if you do multhithreading).
您可以自由地创建新连接。NET 会发现您正在连接一个已经连接的服务器并会使用它。还取决于您的Net.ServicePointManager.DefaultConnectionLimit
数量,它将建立新的连接(如果您执行多线程)。
回答by dmihailescu
I had a similar issue when HttpWebRequest.KeepAlive = true was not enough to keep it alive. The connection stayed on only after I set request.UnsafeAuthenticatedConnectionSharing = true and ServicePointManager.MaxServicePointIdleTime = 100000;//avoid 0 or low values
当 HttpWebRequest.KeepAlive = true 不足以使其保持活动状态时,我遇到了类似的问题。只有在我设置 request.UnsafeAuthenticatedConnectionSharing = true 和 ServicePointManager.MaxServicePointIdleTime = 100000 后,连接才保持;//避免 0 或低值
回答by Rick Strahl
If you're using Http 1.1 you shouldn't be using Keep-Alive as the connection is implicitly Keep-Alive.
如果您使用的是 Http 1.1,则不应使用 Keep-Alive,因为连接是隐式 Keep-Alive。
The HttpWebRequest.KeepAlive
property can be set but if you're sending an Http 1.1 request it will not actually set the header unless you set it to Close.
HttpWebRequest.KeepAlive
可以设置该属性,但如果您发送的是 Http 1.1 请求,它实际上不会设置标头,除非您将其设置为关闭。
Here's another question that has more info: is an HTTP/1.1 request implicitly keep-alive by default?
这是另一个包含更多信息的问题: 默认情况下 HTTP/1.1 请求是否隐式保持活动状态?