Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having some issues download the source of a webpage, I can view the webpage fine in any browser, I can also run a web spider and download the first page no problem. Whenever I run the code to grab the source of that page I always get 403 forbidden error.

As soon as the request is sent the 403 forbidden error is returned. Anyone have any ideas?
C#
string urlAddress = "http://www.brownells.com/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
      Stream receiveStream = response.GetResponseStream();
      StreamReader readStream = null;

.................................

      response.Close();
      readStream.Close();

Appreciate any help.
Posted
Comments
Florian Braun 10-May-15 4:01am    
I'd guess the owner of that website just don't want you to download his pages so he is checking for your useragent.

As said by Florian Braun the site checks the user agent and denies the download with a 403 error. You can try to trick the website, by changing the user agent:
C#
string urlAddress = "http://www.brownells.com/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
//this is the UserAgent string of Firefox 36.0, use your favourite browser's user agent string :)
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
//get response as usual
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
Share this answer
 
This works, hopefully others will benefit from it.

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.UserAgent = @"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36";

request.Accept = @"text/html";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     
if (response.StatusCode == HttpStatusCode.OK)
{
     Stream dataStream = response.GetResponseStream();
     using (StreamReader streamread = new StreamReader(dataStream))
     doc.Load(streamread);
 
Share this answer
 

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