471,066 Members | 969 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

WebClient & Proxy (407 Proxy authentication required)

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
Nov 21 '05 #1
4 29572
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.
"Morten Snedker" wrote:
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

Nov 21 '05 #2
On Thu, 25 Nov 2004 01:33:05 -0800, "Chris Podmore"
<Ch**********@discussions.microsoft.com> wrote:

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
Nov 21 '05 #3
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)
"Morten Snedker" wrote:
On Thu, 25 Nov 2004 01:33:05 -0800, "Chris Podmore"
<Ch**********@discussions.microsoft.com> wrote:

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

Nov 21 '05 #4
On Thu, 25 Nov 2004 03:43:03 -0800, "Chris Podmore"
<Ch**********@discussions.microsoft.com> wrote:
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 :-)
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ily | last post: by
1 post views Thread by =?Utf-8?B?Z2FsbGFu?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.