Download file via HTTP

  • Thread starter Thread starter Jay Ayliff
  • Start date Start date
J

Jay Ayliff

Hello,

I need to write a program in VB.NET that will download a file from the
Internet, given its URL. Is there anything in the .NET framework that will
do this? If not, will the old Internet Transfer control from VB6 do the
trick?

Jay Ayliff
Stalis
 
* "Jay Ayliff said:
I need to write a program in VB.NET that will download a file from the
Internet, given its URL. Is there anything in the .NET framework that will
do this? If not, will the old Internet Transfer control from VB6 do the
trick?

Did you try 'WebClient.DownloadFile'?
 
Hi Herfried,

Many thanks, that works fine on my development system (XP Pro with networked
ADSL router). Then I tried it on the customer system (Win2003 server,
high-speed Internet connection, technology unknown). The program hung for a
while then failed with:

"System.Net.WebException: The underlying connection was closed: Unable to
connect to the remote server"

Pasting the same URL into IE on the same machine successfully downloaded the
file.

Any ideas?

Regards

Jay Ayliff
Stalis Ltd
 
I reckon that must be it, though I can't see anything on the WebClient
object that allows me to specify the Proxy.
 
I got it to work, but I had to use a WebRequest, because WebClient does not
seem to have any way of specifying a Proxy (rather bit of a shortcoming I
feel - this is the easy way to do it, except it may not work).

The more-or-less working function is shown below. I had to create a
WebRequest, passing the URL in the constructor. Next I had to specify the
proxy, giving it the address and port of the proxy server - I would have
liked to be able to tell it to get whatever Internet Explorer uses. Finally
I had to prompt for User ID and Password and give them to the constructor of
a NetworkCredential object that I gave to the WebProxy object. This is not
nice at all, as it is just using the currently logged on user ID and
password.

Can anyone advise:
a) Can I get the WebRequest to use the same Proxy server as IE without
having to specify the address?
b) If not, can I get the new WebProxy object to use the same User ID and
Password of the current user without having to know what they are, or having
to enter them?

Regards

Jay Ayliff
Stalis Ltd

Private Function DownloadFile(ByVal ZipFileURL As String, ByVal DestFile As
String, Optional ByVal ProxyAddress As String = "") As Boolean
Dim wReq As WebRequest = WebRequest.Create(ZipFileURL)
Dim myProxy As WebProxy
Dim UID As String
Dim PWD As String
UID = InputBox("User ID")
PWD = InputBox("Password")
myProxy = New WebProxy(ProxyAddress)
myProxy.Credentials = New NetworkCredential(UID, PWD)
' Attempt to download the file
Dim wRsp As WebResponse = wReq.GetResponse
Dim respStream As Stream = wRsp.GetResponseStream
Dim FS As Stream = File.Open(DestFile, FileMode.Create, FileAccess.Write) '
Stream we are writing to
Dim BS As New BinaryWriter(FS) ' Binary stream for the actual output
Dim Data(2048) As Byte
Dim SIZE As Integer = 2048
Do
SIZE = respStream.Read(Data, 0, Data.Length)
If SIZE > 0 Then
BS.Write(Data, 0, SIZE)
Else
Exit Do
End If
Loop
BS.Flush()
BS.Close()
FS.Close()
wRsp.Close()
Return True
End Function
 
Back
Top