3

I have a requirement like...i want to access an url (login page which is web) from a winforms. i have to pass the credentials to that url and the response should be the contect of the authenticated web page(markup).

I have written a function which will request the url and return the response. but i am getting error code (407)

"Proxy Authentication Required."

Here is my code.

private static void GetPageContent(){
    string url = "https://LoginPage.aspx/";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "GET";
    // If required by the server, set the credentials.
    //request.Proxy.Credentials = CredentialCache.DefaultCredentials;
    request.Credentials = new NetworkCredential("user1", "testuser#");
    // Get the response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    // Display the status.
    Console.WriteLine(response.StatusDescription);
    // Get the stream containing content returned by the server.
    Stream dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Display the content.
    Console.WriteLine(responseFromServer);
    // Cleanup the streams and the response.
    reader.Close();
    dataStream.Close();
    response.Close();
}
B770
  • 1,272
  • 3
  • 17
  • 34
Tim
  • 231
  • 3
  • 4
  • 13

4 Answers4

4
WebProxy proxy = new WebProxy(proxyAddress);
proxy.Credentials = new NetworkCredential("username", "password", "domain");
proxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = proxy;

HttpWebRequest request = new HttpWebRequest();
request.Proxy = proxy;

Or you could use WebClient.

WebClient client = new WebClient();
client.Proxy = proxy;
string downloadString = client.DownloadString("http://www.google.com");
Annie Lagang
  • 3,185
  • 1
  • 29
  • 36
  • Hi Anne, And what would be the proxyAddress?? – Tim Jan 18 '13 at 09:33
  • I passed the uri as the proxy address, but still i m getting error. but this time as "The ServicePointManager does not support proxies with the https scheme." – Tim Jan 18 '13 at 09:37
  • Make sure the url is prefixed with `http://`. Refer to [this](http://blogs.msdn.com/b/jpsanders/archive/2007/04/25/the-servicepointmanager-does-not-support-proxies-of-https-scheme-net-1-1-sp1.aspx) and [this](http://stackoverflow.com/questions/954635/the-servicepointmanager-does-not-support-proxies-of-scheme) – Annie Lagang Jan 18 '13 at 09:43
  • @Tim You got 407 error because you're behind a proxy. To get your proxy address, go to your IE settings > Connection > LAN Settings > Proxy server. – Annie Lagang Jan 18 '13 at 10:12
  • 1
    still getting error "The ServicePointManager does not support proxies with the https scheme." :( – Tim Jan 18 '13 at 10:17
  • Hi Anne, the Proxy server options are enabled in my inetrnet settings....what else need to be done? – Tim Jan 18 '13 at 10:22
  • @Tim Try to set up your Socks settings in IE. IE Settings > Connection > LAN Settings > Proxy server > Check use proxy server > Advanced > Socks. – Annie Lagang Jan 18 '13 at 10:30
  • Socks option is disabled. (and box is empty) – Tim Jan 18 '13 at 10:31
  • @Tim Uncheck Use the same proxy... Then copy your proxy settings from the other boxes to Socks. Or you can use [CNTLM] (http://cntlm.sourceforge.net/) to handle this authentication for you. See this [post](http://stackoverflow.com/questions/14149422/using-pip-behind-a-proxy/14188155#14188155) on how to setup CNTLM. – Annie Lagang Jan 18 '13 at 10:34
  • Thanks guys for your help...still i stuck with the problem. – Tim Jan 18 '13 at 11:53
2

You might want to look at System.Net.HttpWebRequest.Proxy on MSDN.
This gives details of how to set proxy authentication.

There is also a working code sample on this SO answer: https://stackoverflow.com/a/9603791/204690

For example:

// Create a new request to the mentioned URL.               
HttpWebRequest myWebRequest= (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");

// Obtain the 'Proxy' of the  Default browser.  
IWebProxy proxy = myWebRequest.Proxy;

if (proxy != null)
{
    // Create a NetworkCredential object and associate it with the  
    // Proxy property of request object.
    proxy.Credentials=new NetworkCredential(username,password);
    // or 
    proxy.UseDefaultCredentials = true; 

    // try forcing the proxy to use http (just to the proxy not from proxy to server)
    UriBuilder proxyAddress = new UriBuilder(proxy.Address);
    proxyAddress.Scheme = "http";

    myWebRequest.Proxy=proxy;
}
HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse();
Community
  • 1
  • 1
Grhm
  • 6,726
  • 4
  • 40
  • 64
  • Hi, I tried the second one...still getting error..."The ServicePointManager does not support proxies with the https scheme." – Tim Jan 18 '13 at 09:40
  • You might need to force the proxy to be http not https. – Grhm Jan 18 '13 at 09:58
2

For me it was as simple as telling it to use the DefaultCredentials (though I still haven't figured out why it can't use these by default):

request.Proxy.Credentials = (System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials;
David Burton
  • 1,130
  • 10
  • 12
0

You can check if you can connect to the proxy first. Here is an example:

 System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                sock.Connect(url, proxyPort);
                if (sock.Connected == true)  // Port is in use and connection is successful
                {
                    sock.Close();
                    return true;
                }
                else
                {
                    sock.Close();
                }`enter code here`
                return false;
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42