简体   繁体   English

使用Webclient下载多个文件

[英]Use Webclient to download several files

I'm trying to download files from webserver via a NUnit-testcase like this: 我正在尝试通过NUnit测试用例从Web服务器下载文件:

[TestCase("url_to_test_server/456.pdf")]
[TestCase("url_to_test_server/457.pdf")]
[TestCase("url_to_test_server/458.pdf")]
public void Test(string url) 
{
    using (WebClient client = new WebClient())
    {
        client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
        client.DownloadFile(new Uri(url), @"C:\Temp\" + Path.GetFileName(url));
    }
}

This code works, but when i'm trying to get the file size, it hangs. 此代码有效,但是当我尝试获取文件大小时,它挂起了。

[TestCase("url_to_test_server/456.pdf")]
[TestCase("url_to_test_server/457.pdf")]
[TestCase("url_to_test_server/458.pdf")]
public void Test(string url) 
{
    using (WebClient client = new WebClient())
    {
        client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
        client.OpenRead(url);
        Int64 bytes_total = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
        client.DownloadFile(new Uri(url), @"C:\Temp\" + Path.GetFileName(url));
    }
}

How to solve this? 如何解决呢?

You have to make sure that your server supports this header too. 您必须确保您的服务器也支持此标头。 It doesn't seem to be a client problem. 看来这不是客户问题。

I'd download the file in a browser and check the communications using firebug or some similar program. 我会在浏览器中下载文件,并使用萤火虫或类似程序检查通讯。 You have to see Content-length explicitly returned in the response. 您必须看到在响应中显式返回的Content-length。 If not, you need to check the server, otherwise your problem is on the client side. 如果不是,则需要检查服务器,否则问题出在客户端。 I actually can't imagine a reason why the client can't read the header if it is indeed beeing returned. 我实际上无法想象如果客户端确实返回了标题却无法读取标题的原因。

在此处输入图片说明

Waking up dead post but here is the answer... 醒来死了的帖子,但这是答案...

This issue happens when server is not providing Content-Length in the response header. 当服务器未在响应标头中提供Content-Length时,就会发生此问题。 You have to fix that on the server side. 您必须在服务器端进行修复。

Another reason when it happens is when we have reached the connection limit to the server. 发生这种情况的另一个原因是当我们达到服务器的连接限制时。 So I am assuming that your issue was similar and it was hanging on second or third try in a loop. 因此,我假设您的问题是相似的,并且正在循环中进行第二次或第三次尝试。

When we call OpenRead, it opens up a stream. 当我们调用OpenRead时,它将打开一个流。 We just need to close this stream after getting our file size to make it work properly. 我们只需要在获取文件大小后关闭此流,即可使其正常运行。

Here is the code I use to get the size: 这是我用来获取大小的代码:

    /// <summary>
    /// Gets file size from a url using WebClient and Stream classes
    /// </summary>
    /// <param name="address">url</param>
    /// <param name="useHeaderOnly">requests only headers instead of full file</param>
    /// <returns>File size or -1 if their is an issue.</returns>
    static Int64 GetFileSize(string address, bool useHeaderOnly = false)
    {
        Int64 retVal = 0;
        try
        {
            if(useHeaderOnly)
            {
                WebRequest request = WebRequest.Create(address);
                request.Method = "HEAD";

                // WebResponse also has to be closed otherwise we get the same issue of hanging on the connection limit. Using statement closes it automatically.
                using (WebResponse response = request.GetResponse())
                {
                    if (response != null)
                    {
                        retVal = response.ContentLength;
                        //retVal =  Convert.ToInt64(response.Headers["Content-Length"]);
                    }
                }
                request = null;
            }
            else
            {
                using (WebClient client = new WebClient())
                {
                    // Stream has to be closed otherwise we get the issue of hanging on the connection limit. Using statement closes it automatically.
                    using (Stream response = client.OpenRead(address))
                    {
                        retVal = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            retVal = -1;
        }


        return retVal;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM