Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if url leads to a file or a page

Tags:

c#

vb.net

We need the following:

Download a file from an URL if it is actually a file. Otherwise if its a page do nothing.

For a quick example I've got the following to download a file:

 My.Computer.Network.DownloadFile(
"http://www.wired.com/wiredenterprise/wp-content/uploads/2013/07/ff_googleinfrastructure_large.jpg",
"d:\ff_googleinfrastructure_large.jpg")

But if we got a normal web page for example "http://www.google.com" it will just download the page which is something we do not want.

So how can i find out if an URL will lead to a file instead of a page?

It could be any type of file so checking if the URL ends with .zip or .jpg or .docx or... simply won't do.

Answers in VB.NET or C# are both welcome which is why i marked both.

like image 752
Nick V Avatar asked Sep 16 '13 13:09

Nick V


1 Answers

Ahead of time, there's no 100% accurate way. You could check the extension (assuming there is one), but even that is not 100% foolproof.

You could make the request and examine the content-type header and bail out of downloading the file if the value is text/html or some text MIME variant. As olydis points out below, you can perform a HEAD request to just get the response header back and decide then if you want to download the file in its entirety at that point.

like image 119
Moo-Juice Avatar answered Oct 16 '22 18:10

Moo-Juice