How to Check If System.Net.Webclient.Downloaddata Is Downloading a Binary File

How to check if System.Net.WebClient.DownloadData is downloading a binary file?

Given your update, you can do this by changing the .Method in GetWebRequest:

using System;
using System.Net;
static class Program
{
static void Main()
{
using (MyClient client = new MyClient())
{
client.HeadOnly = true;
string uri = "http://www.google.com";
byte[] body = client.DownloadData(uri); // note should be 0-length
string type = client.ResponseHeaders["content-type"];
client.HeadOnly = false;
// check 'tis not binary... we'll use text/, but could
// check for text/html
if (type.StartsWith(@"text/"))
{
string text = client.DownloadString(uri);
Console.WriteLine(text);
}
}
}

}

class MyClient : WebClient
{
public bool HeadOnly { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest req = base.GetWebRequest(address);
if (HeadOnly && req.Method == "GET")
{
req.Method = "HEAD";
}
return req;
}
}

Alternatively, you can check the header when overriding GetWebRespons(), perhaps throwing an exception if it isn't what you wanted:

protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse resp = base.GetWebResponse(request);
string type = resp.Headers["content-type"];
// do something with type
return resp;
}

check to see if URL is a download link using webclient c#

Instead of loading the complete content behind the link, I would issue a HEAD request.

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

Quote of http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

See these questions for C# examples

  • How to check if a file exists on a server using c# and the WebClient class
  • How to check if System.Net.WebClient.DownloadData is downloading a binary file?

How do I check if WebClient download is completed before I execute a function?

The DownloadStringAsync() method is returning before the string is downloaded because it's an asynchronous method. If you move the DoSomethingWithJson() method to your completed handlers then it will be called once the request is completed. You may want to add logic to the DoSomethingWithJson() method so that it only does it's work if all the variables it needs are populated (If indeed you need them all to be populated before you start doing anything else).

WebClient Url1 = new WebClient();
Url1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Url1_DownloadStringCompleted);
Url1.DownloadStringAsync(new Uri("http://example.com"));
WebClient Url2 = new WebClient();
Url2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Url2_DownloadStringCompleted);
Url2.DownloadStringAsync(new Uri("http://anotherexample.com"));
var json1Done = false;
var json2Done = false;

void Url1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
json1 = JObject.Parse(e.Result);
json1Done = true;
if(json1Done && json2Done)
{
DoSomethingWithJson();
}
}

void Url2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
json2 = JObject.Parse(e.Result);
json2Done = true;
if(json1Done && json2Done)
{
DoSomethingWithJson();
}
}

Alternatively, if you are using .Net 4.5 then you could use the new async/await features:

WebClient Url1 = new WebClient();
var json1Task = Url1.DownloadStringTaskAsync(new Uri("http://example.com"));
WebClient Url2 = new WebClient();
var json2Task = Url2.DownloadStringTaskAsync(new Uri("http://anotherexample.com"));

json1 = await json1Task;
json2 = await json2Task;

DoSomethingWithJson();

How can I programmatically tell if a binary file on a website (e.g. image) has changed without downloading it?

You can check that whether the file is changed or not by requesting with HEAD.

Then, returned response header may include Last-Modified, or ETag if the web server support.



Related Topics



Leave a reply



Submit