3
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.example.com");
NetworkCredential nc = new NetworkCredential("myname", "mypass");
WebProxy myproxy = new WebProxy("192.168.1.1:8080", false);
myHttpWebRequest.Proxy = myproxy;
myHttpWebRequest.Proxy = WebRequest.DefaultWebProxy;
myHttpWebRequest.Method = "GET";

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
MessageBox.Show("Ok");

I'm using this code to connect with a website (C#.net desktop application). But I'm having this error message:

The remote server returned an error: (407) Proxy Authentication Required.

How can i fix this?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kevin
  • 407
  • 2
  • 7
  • 22

1 Answers1

2

You're currently not using the credentials in the proxy. Here is an example adapted from MSDN of how to use your NetworkCredential:

class Downloader
{
    static void Main(string[] args)
    {
        NetworkCredential nc = new NetworkCredential(args[0], args[1]);
        WebProxy proxy = new WebProxy(args[2], false);
        proxy.Credentials = nc;

        WebRequest request = new WebRequest(args[3]);
        request.Proxy = proxy;
        using (WebResponse response = request.GetResponse())
        {
            Console.WriteLine(
                @"{0} - {1} bytes",
                response.ContentType,
                response.ContentLength);
        }
    }
}

When I compiled and ran this complete example:

C:\cs>csc proxy.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.


C:\cs>proxy user pass http://proxy:80 http://www.google.com
text/html; charset=ISO-8859-1 - 31398 bytes

C:\cs>

Of course I used my actual user/pass and proxy for my work account.

user7116
  • 63,008
  • 17
  • 141
  • 172
  • Why do you use `CredentialCache`+`NetworkCredential` instead of just `NetworkCredential`? – abatishchev Nov 22 '11 at 07:41
  • Because I adapted it from the wrong example initially. Corrected, thank you. – user7116 Nov 22 '11 at 07:43
  • @six: btw, that was not a pestering, but a curiosity :) – abatishchev Nov 22 '11 at 07:47
  • how im having this error message Invalid URI: The URI scheme is not valid. I have credentials for proxy and as well as username and password for the website i need to be logged in – Kevin Nov 22 '11 at 07:55
  • @Kevin: you likely need to prepend `http://` to the proxy URI. I've done so in my example. – user7116 Nov 22 '11 at 08:00
  • @six : having the previous error message: The remote server returned an error: (407) Proxy Authentication Required. – Kevin Nov 22 '11 at 08:03
  • @Kevin: if you haven't already done so, you need to remove `myHttpWebRequest.Proxy = WebRequest.DefaultWebProxy;`. – user7116 Nov 22 '11 at 08:06
  • as i have a password and a username for the proxy do i have to add proxy credentials w ebRequest.Proxy.Credentials = new NetworkCredential("","","");? – Kevin Nov 22 '11 at 08:06
  • i executed the code without myHttpWebRequest.Proxy = WebRequest.DefaultWebProxy; but still having the same error – Kevin Nov 22 '11 at 08:07
  • @Kevin: I've updated my answer to include a complete sample that I've run on my machine right now at work, which sits behind a proxy. – user7116 Nov 22 '11 at 08:15
  • Are there a password and a username for your proxy. i have a password and username for the proxy. Still cant loggin – Kevin Nov 22 '11 at 08:59