Link to home
Start Free TrialLog in
Avatar of SirCaleb
SirCaleb

asked on

System.Net.WebClient w/ Proxy Servers

I cannot seem to find a way to make the system.net.webclient work through a proxy server.  Web Services has a proxy member but webclient does not ... odd.

Anyone have any reasonable idea on how to make the System.Net.WebClient work through a proxy server?

Dim wc As New System.Net.WebClient
wc.DownloadFile("http://www.something.com/whatever.txt", "C:\whatever.txt")

Doing this through a proxy throws the following exception:

System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
   at System.Net.HttpWebRequest.CheckFinalStatus()
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.HttpWebRequest.GetResponse()
   at System.Net.WebClient.DownloadFile(String address, String fileName)

I have Web Services working through proxies with the following code ...

Dim ws As com.whatever.www.myWebService
Dim proxyObject As New System.Net.WebProxy("myProxyServer", 8080)
proxyObject.BypassProxyOnLocal = True
proxyObject.Credentials = System.Net.CredentialCache.DefaultCredentials
ws.Proxy = proxyObject

Surprising that the System.Net.WebClient does not support this same member.
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SirCaleb
SirCaleb

ASKER

Hrm, that did not fix my problem :(

It's still not authenticating on the proxy server ...

Dim wc As New System.Net.WebClient
wc.Credentials = System.Net.CredentialCache.DefaultCredentials
wc.DownloadFile(DownloadURL, DownloadPath)

returns:

System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
   at System.Net.HttpWebRequest.CheckFinalStatus()
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.HttpWebRequest.GetResponse()
   at System.Net.WebClient.DownloadFile(String address, String fileName)

I am using the credentialcache in the exact same way for the web services and it fixed them ... but not the webclient.  Ideas?
Actually, that did work but MSDN needs to update their documentation.  If you use the globalproxyselection, you have to set the credentials there and not on the webservice or webclient objects:

Dim proxyObject As New WebProxy("myServer", 8080)
proxyObject.BypassProxyOnLocal = True
proxyObject.Credentials = CredentialCache.DefaultCredentials
GlobalProxySelection.Select = proxyObject

Thanks for the help.
correct.

The ones on the webclient would override this.