问题描述
我在查找如何关闭 WebClient
所做的连接时遇到问题。我创建了一个新的 WebClient
对象,然后多次调用 DownloadFile
方法,但是,它始终会创建一个新连接对于每个调用并且这些连接保持打开状态(已建立状态),我可以在TCPView中看到所有已建立的连接。
I'm having a problem finding out how to close the connection made by WebClient
. I create a new WebClient
object and then call DownloadFile
method a lot of times, however, it always creates a new connection for each call and those connections stay open (Established state), I can see in TCPView all the established connections.
如果处理<$时出现的问题甚至更多c $ c> Webclient ,他们保持建立......?
如何在下载完成后强制关闭连接?
What bugs me even more if when I dispose the Webclient
, they stay established...?How to force the connection to be closed after the download is done?
我已经尝试派生WebClient并将keep alive手动设置为false,我的应用程序配置也允许足够的连接。
I already tried to derive WebClient and set keep alive manually to false, my app config allow enough connections as well.
<connectionManagement>
<add address="*" maxconnection="1000"/>
</connectionManagement>
推荐答案
简答:你不应该关闭手动连接。它们在幕后为您管理。
Short answer: you shouldn't need to close the connections manually. They are managed for you behind the scenes.
请求完成后,HTTP / 1.1连接不会立即关闭,以便在同一服务器中处理多个请求更及时有效的方式(例如Web浏览器从单个站点请求多个文件)。您不必担心这一点或手动关闭它们,因为它们会在一段时间后超时。是否导致错误?
HTTP/1.1 connections are not closed as soon as the request completes in order that multiple requests to the same server are handled in a more timely and efficient manner (such as a web browser requesting multiple files from a single site). You shouldn't have to worry about this or close them down manually, as they'll timeout after a while. Is it causing an error?
如果是一个问题,您可以尝试继承 WebClient
并覆盖 GetWebRequest
手动设置 KeepAlive
的方法,例如:
If it is an issue, you could try inheriting from WebClient
and overriding the GetWebRequest
method to manually set KeepAlive
, e.g.:
public class NoKeepAlivesWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
((HttpWebRequest)request).KeepAlive = false;
}
return request;
}
}
我也总是建议使用使用模式 WebClient
:
I'd also always suggest using the using pattern for WebClient
:
using (var client = new NoKeepAlivesWebClient())
{
// Some code
}
最后,这里有一些有关HTTP / 1.1中持久连接的RFC信息:
Finally, here's some RFC information about persistent connections in HTTP/1.1:
以及更友好的维基百科条目:
and a friendlier Wikipedia entry:
编辑:
道歉。我从你编辑过的问题中看到你已经尝试了类似上面的内容,但没有成功。
Apologies. I see from your edited question that you've already tried something like the above, without success.
然而,我无法重现你的问题。根据TCPView,我使用 NoKeepAlivesWebClient
编写了一个小程序,它在使用后成功关闭了连接。
I was unable to reproduce your issue, however. I wrote a small program using the NoKeepAlivesWebClient
and it successfully closed connections after they were used, according to TCPView.
static void Main(string[] args)
{
// Random test URLs
var urls = new List<string> {
"http://msdn.microsoft.com/en-us/library/tt0f69eh.aspx",
"http://msdn.microsoft.com/en-us/library/system.net.webclient.allowreadstreambuffering.aspx",
"http://msdn.microsoft.com/en-us/library/system.net.webclient.allowwritestreambuffering.aspx",
"http://msdn.microsoft.com/en-us/library/system.net.webclient.baseaddress.aspx",
"http://msdn.microsoft.com/en-us/library/system.net.webclient.cachepolicy.aspx",
"http://msdn.microsoft.com/en-us/library/system.net.webclient.credentials.aspx",
"https://www.youtube.com/",
"https://www.youtube.com/feed/UClTpDNIOtgfRkyT-AFGNWVw",
"https://www.youtube.com/feed/UCj_UmpoD8Ph_EcyN_xEXrUQ",
"https://www.youtube.com/channel/UCn-K7GIs62ENvdQe6ZZk9-w" };
using (var client = new NoKeepAlivesWebClient())
{
// Save each URL to a Temp file
foreach (var url in urls)
{
client.DownloadFile(new Uri(url), System.IO.Path.GetTempFileName());
Console.WriteLine("Downloaded: " + url);
}
}
}
这里还有另外一个SO问题相同的问题:
There's another SO question here about the same issue:
这篇关于WebClient每次下载文件时都会打开一个新连接,并且所有文件都保持建立状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!