4

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();
Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
Tim
  • 231
  • 3
  • 4
  • 13
  • 2
    On a separate note, I'd recommend using `using` statements around your `IDisposable` variables rather than calling `Close` explicitly. – Daniel Kelley Jan 21 '13 at 08:45

3 Answers3

2

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.

Magnus
  • 980
  • 4
  • 10
  • Hi Magnus, i jst disabled the proxy in my machine and my internet is not working. Proxy is required to run the internet. – Tim Jan 21 '13 at 08:51
  • Hey Tim. Sounds like you'll have to add the appropriate authentication headers, then. I see there's already an answer that should cover this here: http://stackoverflow.com/questions/9603093/407-proxy-authentication-required-in-c-sharp – Magnus Jan 21 '13 at 08:55
  • Magnus, mine is winforms appl. i hv added App.config file to my proj and added the Configuration settings to that file and try to run that, still getting same error. :( – Tim Jan 21 '13 at 09:04
  • According to the answer and the whole thread you'll have to use that IWebProxy object, too. WinForms vs. any other kind of app shouldn't make any difference either since it's really all about the HttpWebRequest part. – Magnus Jan 21 '13 at 09:18
2

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()); 
        }
    }
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Anwuna
  • 1,077
  • 11
  • 18
0
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");

}
Kishan_KP
  • 4,488
  • 6
  • 27
  • 46
Ronel Gonzales
  • 125
  • 1
  • 7