Tim
Tim

Reputation: 241

Remote Server returned an error: (407) Proxy Authentication Required

I am trying to open a url from my winforms application and i am getting "407 Proxy Authentication Required" error. I am able to open a sample application that was deployed on IIS in my machine. but if i try to access any other URL getting this error. Here is the source code. Any suggestions please.

string url = "http://google.co.in";


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Console.WriteLine(response.StatusDescription);

Stream dataStream = response.GetResponseStream();

StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();

Console.WriteLine(responseFromServer);
MessageBox.Show(responseFromServer);

reader.Close();
dataStream.Close();
response.Close();

Upvotes: 4

Views: 34345

Answers (3)

Ronel Gonzales
Ronel Gonzales

Reputation: 125

IWebProxy proxy = new WebProxy("proxy address", port number); 
proxy.Credentials = new NetworkCredential("username", "password");

using (var webClient = new System.Net.WebClient())
{
    webClient.Proxy = proxy;

    webClient.DownloadString("url");

}

Upvotes: 0

Anwuna
Anwuna

Reputation: 1113

Try this, it worked for me

string url = "http://google.co.in"; 
IWebProxy proxy = new WebProxy("your proxy server address", port number ); // port number is of type integer 
        proxy.Credentials = new NetworkCredential("your user name", "your password");

        try
        {
                WebClient client = new WebClient();
                client.Proxy = proxy;

                string resp = client.DownloadString(url);
                // more processing code 
                }
            }
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.ToString()); 
        }
    }

Upvotes: 2

Magnus
Magnus

Reputation: 980

That would indicate that the proxy set in your system settings requires you to log in before you're able to use it. If it works in your browser, you've most likely done so in the past. Either disable the proxy in your system's network settings, or add the appropriate headers to authenticate against the proxy.

Upvotes: 2

Related Questions