WebProxy error: Proxy Authentication Required
Asked Answered
T

3

14

I use the following code to obtaing html data from the internet:

WebProxy p = new WebProxy("localproxyIP:8080", true);
p.Credentials = new NetworkCredential("domain\\user", "password");
WebRequest.DefaultWebProxy = p;
WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.google.com");

But the following error is appeared: "Proxy Authentication Required". I can't use default proxy because of my code runs from windows service under the special account which there is no default proxy settings for. So, I want to specidy all proxy settings in my code. Please advice me how to resolve this error.

Tunstall answered 22/10, 2012 at 9:58 Comment(1)
try to remove the domain\\ part (also remember of wrapping WebClient with using() statement because it implements IDisposable)Shrader
M
21

You've to set the WebClient.Proxy Property..

WebProxy p = new WebProxy("localproxyIP:8080", true);
p.Credentials = new NetworkCredential("domain\\user", "password");
WebRequest.DefaultWebProxy = p;
WebClient client = new WebClient();
**client.Proxy = p;**
string downloadString = client.DownloadString("http://www.google.com");
Milieu answered 22/10, 2012 at 10:8 Comment(4)
it's not needed because WebRequest.DefaultWebProxy = p is enoughShrader
you're right if you use WebRequest.Create() and after you call WebRequest.GetResponse(). But in this case he's using the WebClient to make request so the Proxy is not the same.Milieu
Actually DownloadString method internally uses WebRequest object created by WebRequest.Create(). If proxy wasn't particularly set to WebClient object, such WebRequest object uses proxy obtained from WebRequest.InternalDefaultWebProxy property, which is just set by WebRequest.DefaultWebProxy = p statement;Shrader
Imho solution below https://mcmap.net/q/789872/-webproxy-error-proxy-authentication-required is better.Batts
G
52

This worked for me:

IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
client = new WebClient
    {
        Proxy = defaultWebProxy
    };
string downloadString = client.DownloadString(...);
Grati answered 19/6, 2013 at 9:2 Comment(3)
Wonderful! This should be part of every WebClient sample.Fachini
Can confirm this answer is perfect - We distributed software to clients that kept hitting this issue due to network restrictions. This patched the single issue everyone was facing. This should be the selected answer. YOU SAVED MEMelodious
I use Console dotnet Core, how can i config it in setting file (such as app.config in Net Framework)Territorial
M
21

You've to set the WebClient.Proxy Property..

WebProxy p = new WebProxy("localproxyIP:8080", true);
p.Credentials = new NetworkCredential("domain\\user", "password");
WebRequest.DefaultWebProxy = p;
WebClient client = new WebClient();
**client.Proxy = p;**
string downloadString = client.DownloadString("http://www.google.com");
Milieu answered 22/10, 2012 at 10:8 Comment(4)
it's not needed because WebRequest.DefaultWebProxy = p is enoughShrader
you're right if you use WebRequest.Create() and after you call WebRequest.GetResponse(). But in this case he's using the WebClient to make request so the Proxy is not the same.Milieu
Actually DownloadString method internally uses WebRequest object created by WebRequest.Create(). If proxy wasn't particularly set to WebClient object, such WebRequest object uses proxy obtained from WebRequest.InternalDefaultWebProxy property, which is just set by WebRequest.DefaultWebProxy = p statement;Shrader
Imho solution below https://mcmap.net/q/789872/-webproxy-error-proxy-authentication-required is better.Batts
T
1

Try this code

var transferProxy = new WebProxy("localproxyIP:8080", true);
transferProxy.Credentials = new NetworkCredential("user", "password", "domain");
var transferRequest = WebRequest.Create("http://www.google.com");
transferRequest.Proxy = transferProxy;
HttpWebResponse transferResponse = 
    (HttpWebResponse)transferRequest.GetResponse(); 
System.IO.Stream outputStream = transferResponse.GetResponseStream();
Talishatalisman answered 22/10, 2012 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.