Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an application that extracts data from a webpage. the organization sometimes uses a proxy server for internet connection or sometimes it odes it simply, no authentication required

1) How do I detect whether proxy is enabled, if yes then how do I connect to the specific webpage

I used the following code to connect to the webpage using proxy, but it returns error 407.
C#
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://www.google.com");

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=student";
postData += "&password=student";

webRequest.Proxy = new WebProxy("http://10.10.16.1:3434/", true); 
httpWReq.Proxy = WebRequest.DefaultWebProxy;
httpWReq.Credentials = new NetworkCredential("student", "student", "http://www.google.com");
Posted
v2
Comments
Try to connect websites other than Google.
Member 8657306 31-Jul-13 11:08am    
tried, same error
Dave Kreskowiak 31-Jul-13 11:52am    
407 is "Proxy authentication required". SO, that request would be going through the proxy and your not authorized to do that using your default credentials.

1 solution

The following can be used for proxy detection:

C#
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://www.google.com");
WebProxy p = (WebProxy) WebRequest.DefaultWebProxy;

if (proxy.Address.AbsoluteUri != string.Empty)
{
    // If the proxy is mentioned in the browser
    httpWReq.Proxy = proxy;
}
else
{
    ICredentials cred;
    cred = new NetworkCredential("student", "student");
    p = new WebProxy("http://10.10.16.1:3434/", true, null, cred); 
    httpWReq.Proxy = p;
}


ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=student";
postData += "&password=student";
// rest of your code
 
Share this answer
 
v2
Comments
Member 8657306 2-Aug-13 13:28pm    
the following code will enable proxy connection to the website, bt how do i detect that proxy exists & include the default proxy settings from internet settings???
Srinivas Kalabarigi 2-Aug-13 22:27pm    
I updated the solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900