MePengusta
MePengusta

Reputation: 943

Getting "The remote server returned an error: (403) Forbidden" Error Whatever I Do

I'm getting "The remote server returned an error: (403) Forbidden." error everytime i make a request to a website.I also tried every single header i saw on browser while making a request and i'm still getting the same error.What should i do to fix this error ? This is a console application on .NET Core 3.

        var url = "my-url";
        var client = new WebClient();
        client.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
        client.Headers.Add("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); 
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        var html = client.DownloadString(url);

Unhandled exception. System.Net.WebException: The remote server returned an error: (403) Forbidden.

Upvotes: 2

Views: 9329

Answers (1)

Stelios Giakoumidis
Stelios Giakoumidis

Reputation: 2283

The reason that is happening is very likely some web scraping countermeasures implemented by the site you target.

In order to by pass it you should try to impersonate a real browser as much as possible. That would happen by setting the necessary headers.

client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml");
client.Headers.Add("Accept-Encoding", "gzip, deflate");
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
client.Headers.Add("Accept-Charset", "ISO-8859-1");

The above are some basic headers used by the browsers, try them and if it still does not work I would suggest to continue focusing on that direction.

Step2:

Visit this website from your browser using any sort of network proxy, like Fiddler, and intercept the headers on the request. Then mimic them in your application. That should work 100%.

Upvotes: 2

Related Questions