WebClient & Proxy (407 Proxy authentication required)

M

Morten Snedker

I'm trying to download a file. I've tried both using webclient and the
httpWebRequest. Either way it returns a "407 proxy authentication
required".

The program is to be run externally, so basically I don't know the
users login-name, password or domain. So that makes setting the proxy
in code irrelevant, doesn't it?

When searching the web it appears to be a common problem. But I
haven't found my solution and still can't get it to work...

This the code I'm trying to execute:

'---
Function DownloadVersionFile() As Boolean

LocalVersionfile = Application.ExecutablePath
LocalVersionfile = LocalVersionfile.Substring(0,
LocalVersionfile.Length - 19) & "vk.txt"

Dim wctl As New WebClient
Try
wctl.DownloadFile(versionFile, LocalVersionfile)
DownloadVersionFile = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fejl i
DownloadVersionFile")
DownloadVersionFile = False
End Try

End Function
'----


Another part of my code that fails with same error:


'---
Dim myURI As Uri, url As String

url = FileToDownload()
myURI = New Uri(url)

Try
Dim obj_HTTPWebRequest As HttpWebRequest =
CType(HttpWebRequest.Create(url), HttpWebRequest)
obj_HTTPWebRequest.Credentials =
CredentialCache.DefaultCredentials
Dim obj_HTTPWebResponse As HttpWebResponse =
obj_HTTPWebRequest.GetResponse()
lFileSize = (obj_HTTPWebResponse.ContentLength)
Catch ex As Exception
MsgBox(Err.Number)
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fejl i
DownloadNewVersionFile")
Application.Exit()
End Try

'---


Thx in advance!

Regards /Snedker
 
G

Guest

I have tried your sample code here at work and I get the 407 as well. I think
you are going to have to specify the proxy setting, I don't know of anyway
round this.

Try:

Dim proxyObject As New System.Net.WebProxy("http://999.999.999.999:9999",
True)
obj_HTTPWebRequest.Proxy = proxyObject
obj_HTTPWebRequest.Proxy.Credentials =
System.Net.CredentialCache.DefaultCredentials

Replace 999.999.999.999:9999 with your proxy IP address and port number.

Hope this helps.
Chris.
 
M

Morten Snedker

On Thu, 25 Nov 2004 01:33:05 -0800, "Chris Podmore"

First of all, thanks for your help so far. I've gotten a bit further
since now I don't get any errors back from the server.

But...it just won't download the file. If I run it at home it works
fine, but from work through ISA and proxy, it doesn't.

I've tried setting the proxy explicitly with
Dim proxyObject As New System.Net.WebProxy("http://PICASSO:8080",
True), but this doesn't help either.

Trying to read the filesize with ContentLength, -1 is returned. This
is the code:

Dim myURI As Uri, url As String
url = versionFile
myURI = New Uri(url)

LocalVersionfile = Application.ExecutablePath
LocalVersionfile = LocalVersionfile.Substring(0,
LocalVersionfile.Length - 19) & "vk.txt"

Try
Dim proxyObject As New WebProxy
proxyObject.GetDefaultProxy()

Dim obj_HTTPWebRequest As HttpWebRequest =
CType(HttpWebRequest.Create(url), HttpWebRequest)

With obj_HTTPWebRequest
.Proxy = proxyObject
.Proxy.Credentials =
CredentialCache.DefaultCredentials()
Dim obj_HTTPWebResponse As HttpWebResponse =
..GetResponse()
lFileSize = (.ContentLength)
End With

Catch ex As Exception
MsgBox(ex.Message)
Application.Exit()
End Try

MsgBox("Filesize: " & lFileSize)


Thanks again. Your help is greatly appreciated!

/Snedker
 
G

Guest

I'm at a bit of a loss as to what your problem is now. We have ISA etc and
the following code works fine for me.

Sorry I can't be more help.
Chris.

Dim url As String = "http://www.microsoft.com"
Dim obj_HTTPWebRequest As HttpWebRequest = CType(HttpWebRequest.Create(url),
HttpWebRequest)

'Remove these two lines to get 407 error
obj_HTTPWebRequest.Proxy = System.Net.WebProxy.GetDefaultProxy
obj_HTTPWebRequest.Proxy.Credentials =
System.Net.CredentialCache.DefaultCredentials

Dim r As WebResponse = obj_HTTPWebRequest.GetResponse()

MsgBox(r.ContentLength.ToString)
 
M

Morten Snedker

I'm at a bit of a loss as to what your problem is now. We have ISA etc and
the following code works fine for me.
Sorry I can't be more help.

Don't be - you've helped plenty !! :)
Dim r As WebResponse = obj_HTTPWebRequest.GetResponse()

I had
Dim r As HttpWebResponse = .GetResponse()
which is why it didn't work.

But with your help in regards to the proxy, and my little typo, I'm on
the right track...and all is working!

Thanks a lot!


Mr. Happy-Dude :)
 

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

Top