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?

1
  • Do you need to pass nc into the WebProxy constructor? Commented Nov 22, 2011 at 7:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Why do you use CredentialCache+NetworkCredential instead of just NetworkCredential?
Because I adapted it from the wrong example initially. Corrected, thank you.
@six: btw, that was not a pestering, but a curiosity :)
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: you likely need to prepend http:// to the proxy URI. I've done so in my example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.