I am trying to download files from a web server through NUnit-testcase as follows:
[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 try to get the file size, it freezes.
[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?
source
share