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.

Oleg Ignatov
  • 877
  • 2
  • 8
  • 22
  • 1
    try to remove the domain\\ part (also remember of wrapping `WebClient` with `using()` statement because it implements `IDisposable`) – jwaliszko Oct 22 '12 at 10:15

3 Answers3

52

This worked for me:

IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
client = new WebClient
    {
        Proxy = defaultWebProxy
    };
string downloadString = client.DownloadString(...);
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
  • 5
    Wonderful! This should be part of every WebClient sample. – Brad Bruce Jul 16 '13 at 17:25
  • 1
    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 ME – amartin94 Sep 27 '18 at 01:51
  • I use Console dotnet Core, how can i config it in setting file (such as app.config in Net Framework) – KevinBui Jul 14 '20 at 04:52
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");
2GDev
  • 2,478
  • 1
  • 20
  • 32
  • it's not needed because `WebRequest.DefaultWebProxy = p` is enough – jwaliszko Oct 22 '12 at 10:11
  • 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. – 2GDev Oct 22 '12 at 20:28
  • 2
    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; – jwaliszko Oct 22 '12 at 21:12
  • Imho solution below https://stackoverflow.com/a/17187136/7225096 is better. – Peska Jan 29 '18 at 11:30
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();
Marco
  • 56,740
  • 14
  • 129
  • 152