Home All Groups Group Topic Archive Search About

WebRequest: only get info about file, not download the file?

Author
8 May 2006 3:32 PM
Roland_Müller
Hello Group,

i want to get the file size of some files located on a webserver,
therefore i create a WebRequest with the URI of the file and then a
WebResponse on the WebRequest to get the ContentLength:


WebRequest webRequest = HttpWebRequest.Create(fileUri);
webRequest.Method = "HEAD"; //??????? (*)
WebResponse webResponse = webRequest.GetResponse();
fileSizeBytes = (ulong)webResponse.ContentLength;


(*) Does this setting make the WebRequest only send the info and not the
complete file?

I ask because i don't want to download large files completely only to
get the filename.

Thank you very much in advance,
Roland

Author
9 May 2006 7:54 PM
Joerg Jooss
Thus wrote Roland,

> Hello Group,
>
> i want to get the file size of some files located on a webserver,
> therefore i create a WebRequest with the URI of the file and then a
> WebResponse on the WebRequest to get the ContentLength:
>
> WebRequest webRequest = HttpWebRequest.Create(fileUri);
> webRequest.Method = "HEAD"; //??????? (*)
> WebResponse webResponse = webRequest.GetResponse();
> fileSizeBytes = (ulong)webResponse.ContentLength;
> (*) Does this setting make the WebRequest only send the info and not
> the complete file?

WebRequest is a, um, request ;-)
The file will be part of the response.

>
> I ask because i don't want to download large files completely only to
> get the filename.

A HEAD request is similar to a GET request, but the server will not send
a HTTP body, only the headers. If these headers contain Content-Length, you'll
get what you need.

Cheers,
--
Joerg Jooss
news-re***@joergjooss.de
Author
10 May 2006 6:57 AM
Roland_Müller
Thanks Joerg, i did analyze it with a network trace tool and it does
exactly what i want.

Joerg Jooss schrieb:
Show quote
> Thus wrote Roland,
>
>> Hello Group,
>>
>> i want to get the file size of some files located on a webserver,
>> therefore i create a WebRequest with the URI of the file and then a
>> WebResponse on the WebRequest to get the ContentLength:
>>
>> WebRequest webRequest = HttpWebRequest.Create(fileUri);
>> webRequest.Method = "HEAD"; //??????? (*)
>> WebResponse webResponse = webRequest.GetResponse();
>> fileSizeBytes = (ulong)webResponse.ContentLength;
>> (*) Does this setting make the WebRequest only send the info and not
>> the complete file?
>
> WebRequest is a, um, request ;-)
> The file will be part of the response.
>
>>
>> I ask because i don't want to download large files completely only to
>> get the filename.
>
> A HEAD request is similar to a GET request, but the server will not send
> a HTTP body, only the headers. If these headers contain Content-Length,
> you'll get what you need.
>
> Cheers,

AddThis Social Bookmark Button