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?
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.