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