Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I try to create simple WebRequest through Proxy server but always receive the error:

"The remote server returned an error: (403) Forbidden."

My code is as below

try
       {
           WebRequest req = WebRequest.Create(@"http://google.com");
           //if (req.Proxy != null)
           //{
           //    req.Proxy.Credentials = CredentialCache.DefaultCredentials;
           //}
           //string MyProxyHostString = "fsoft-proxy";
           //int MyProxyPort = 8080;
           //req.Proxy = new WebProxy(MyProxyHostString, MyProxyPort);
           WebResponse resp = req.GetResponse();
           Stream strm = resp.GetResponseStream();
           StreamReader reader = new StreamReader(strm);
           string locale = reader.ReadToEnd();
       }
       catch (Exception exp)
       {
           MessageBox.Show(exp.Message);
       }


One thing is that my proxy server's policy does not allow POST method.
and I can access webs in browser without any problems.

So I wonder if proxy's policy cause 403 error?
And is there any way to fix it out?

Thanks for your reading and help.
Posted
Comments
Andrew Brock 13-Dec-11 7:37am    
Blocking POST seems a bit silly, but there is no post data on the google homepage, so this shouldn't be an issue.

try adding a "www." in the url, see if that helps.
Another thing you can try is SSL, "https://encrypted.google.com" as the server. This encrypts your data and essentially bypasses the proxy and whatever content filtering it has since it cannot see any of the data, only that you are connecting to google.

All 3 URLs ("http://google.com", "http://www.google.com", and "https://encrypted.google.com") work on my computer, which has no proxy.
hieund2 14-Dec-11 22:33pm    
I have found the answers for my question.
The problem is we need to add UserAgent to WebRequest, something like this:

WebRequestObject.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24";

because proxy server does not allow request from application other than browser.

Thanks for your reply.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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