Example of HTTP file transfer

  • Thread starter Thread starter BobAchgill
  • Start date Start date
B

BobAchgill

Can you point me to a simple VB .NET example of how to
receive a file from a web server using a HTTP method
available in .NET

Bob
 
Hi,

Here is a quick example. Add these import statements.

Imports System.Net

Imports System.IO



Sample code



Dim request As WebRequest

Dim response As WebResponse

Dim reader As Stream

Dim writer As Stream

Dim data(1023) As Byte

Dim count As Integer

Dim total As Integer

Me.Show()

Me.Text = "Downloading file ....."

Application.DoEvents()

request =
WebRequest.Create("http://www.onteorasoftware.com/downloads/multigrids.zip")

response = request.GetResponse()

reader = response.GetResponseStream()

ProgressBar1.Maximum = CInt(response.ContentLength)

ProgressBar1.Value = 0

total = 0

writer = File.Create("mylocaldata.zip")

While True

count = reader.Read(data, 0, 1024)

If count <= 0 Then

Exit While

End If

writer.Write(data, 0, count)

total += 1024

If total < ProgressBar1.Maximum Then ProgressBar1.Value = total

Application.DoEvents()

End While

reader.Close()

writer.Close()



Ken

--------------------------

Can you point me to a simple VB .NET example of how to
receive a file from a web server using a HTTP method
available in .NET

Bob
 
Beautiful! Thanks Ken.

Is it possible to get a files detail list, too, with HTTP?

But, hey, I am so thankful to get what you gave... it helps me out a
whole lot!

Bob
 
Bob,

I was assuming this was a double post with the webservice and it (is) was
not clear for me which you asked.

This one is a very short one for what you ask about a http webserver.
\\\\
webclient.downloadfile(url,filePath)
////
I hope this helps?

Cor
 
So as I understand the only difference between the two methods for
downloading a file from an HTTP web site is:
- webclient.downloadfile(url,filePath) is short and sweet and to
the point for getting the file down
- WebRequest.Create("url") lets you also do things like monitor
progress or choose to work Asyn

Do either have the ability to see the file datestamp on the server? I
guess that is asking to much of HTTP. Only FTP does datestamps.

Bob
 
Bob,

To get information. It is changed in this message so when you have errors
try to correct it or reply.
There are a lot of webheaders as you see as "content-length"

\\\
dim conlength as string
Dim wbRq As DirectCast(WebRequest.Create(item.Text), HttpWebRequest)
wbRq.Timeout = 2000
Try
Dim wbRs As 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-length" Then
conlenght = values(0)
End If
Next
wbRs.Close()
Catch
conlength = "?"
End Try
///
I hope this helps?

Cor
 
Bob,

Herfried saw that I used VB 2007

:-)

Here is a sample that is tested

You can see in it today that it is the last updated at
Last-Modified Tue, 01 Feb 2005 11:08:47 GMT

\\\
Public Class Herfrieds
Public Shared Sub Main()
Dim conlength As String
Dim wbRq As Net.HttpWebRequest = _
DirectCast(Net.WebRequest.Create _
("http://dotnet.mvps.org/"), Net.HttpWebRequest)
wbRq.Timeout = 2000
Try
Dim wbRs As Net.HttpWebResponse = DirectCast(wbRq.GetResponse(),
_
Net.HttpWebResponse)
Dim wbHCol As Net.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 Then
Console.Write(header)
Console.Write(" ")
Console.Write(values(0))
Console.Write(vbCrLf)
End If
Next
wbRs.Close()
Catch
Console.Write("could not get something")
End Try
End Sub
End Class
///
I hope this helps a little bit better?

:-)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Example of webservice file transfer 5
Modify *.lnk File? 6
View Web cam in application 2
Screen2Video 3
WSDL+SOAP get resulting Xml 3
WCF Performance Issue 2
Call .Net Method from VC++ 15
Decompressing zip files 2

Back
Top