HHTP file download:

  • Thread starter Thread starter Bryan Martin
  • Start date Start date
B

Bryan Martin

Ok im trying to figure out how internet explorers file download box always
seem to at least come close to knowing the exact file size/time to download.
From what I can tell its getting its not getting its info from the
content-length. I know this because I can test query against a apache box
that does not return the content length however IE still knows the size of
the files.

What am i missing?

Bryan
 
Hi Bryan,

I do not believe that you get an answer in a newsgroup what code Internet
Explorer internaly uses.

But what do you want to archieve?

Cor
 
You mean I cant download the complete internet explorer source code? I was
not asking for the code only for how they achieved it. I am downloading
files from the internet but due to some files being large I need a way to
display a progress. Again as the original question implies does anyone know
where the dialog is getting a whiff of a file size from?

Bryan
 
Hi Bryan,

I am not completly sure of it, but I have distributed this before (I have
this in a program, but I had to delete a lot to make it only for what you
ask)..

\\\not tested as is
dim conlength as string
Dim wbRq As HttpWebRequest = DirectCast(WebRequest.Create(item.Text),
HttpWebRequest)
wbRq.Timeout = 2000
Try
Dim wbRs As HttpWebResponse = DirectCast(wbRq.GetResponse(),
HttpWebResponse)
Dim wbHCol As WebHeaderCollection = wbRs.Headers
For i As Integer = 0 To wbHCol.Count - 1
Dim header As String = wbHCol.GetKey(i)
Dim values As String() = wbHCol.GetValues(header)
If values.Length > 0 andalso header.Tolower = "content-lenght" Then
conlenght = values(0)
End If
Next
wbRs.Close()
Catch
conlength = "?"
End Try
///
I hope this helps a little bit?

Cor
 
Cor, thanks again for the response however you have missed the question
again. I know you can get the content length from the headers, matter of
fact there is a much easier way to do it than the example you gave. Problem
is not all servers send a content length so I would like to know how for
instance internet explorer's download dialog knows the file size so it can
give you a idea of how long its gonna take you to download the file. Again
as stated I have downloaded files from a server I know is not giving a
content length and internet explorer's dialog box still correctly displays
the time/progress while downloading.

BTW the httpwebresponse has a "contentlength" method which will return the
value without having to loop through all headers.

Bryan

Dim str_Site As String = "http://download.winzip.com/winzip81.exe"
Dim obj_HTTPWebRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(str_Site)
Dim obj_HTTPWebResponse As System.Net.HttpWebResponse =
obj_HTTPWebRequest.GetResponse
Debug.WriteLine(obj_HTTPWebResponse.ContentLength)
 
Well, I've never done this with VB.NET, but in using C++ and windows sockets
I simply sent a HEAD request to the server to get the content length before
requesting the file with the GET method.

Here is some information from the HTTP RFC concerning content length and
what to do if a content length header is not returned....

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

14.13 Content-Length
The Content-Length entity-header field indicates the size of the
entity-body, in decimal number of OCTETs, sent to the recipient or, in the
case of the HEAD method, the size of the entity-body that would have been
sent had the request been a GET.

Content-Length = "Content-Length" ":" 1*DIGIT
An example is

Content-Length: 3495
Applications SHOULD use this field to indicate the transfer-length of the
message-body, unless this is prohibited by the rules in section 4.4.

Any Content-Length greater than or equal to zero is a valid value. Section
4.4 describes how to determine the length of a message-body if a
Content-Length is not given.

Note that the meaning of this field is significantly different from the
corresponding definition in MIME, where it is an optional field used within
the "message/external-body" content-type. In HTTP, it SHOULD be sent
whenever the message's length can be determined prior to being transferred,
unless this is prohibited by the rules in section 4.4.

Here's a link to section 4.4 that's mentioned....

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
 
Back
Top