httpwebrequest: WebException return "the remote server returned an error 403 forbidden".

Raja Pallavan 1 Reputation point
2023-02-17T14:30:18.53+00:00

Am I trying to create a URI validation tool in C#.net console application.

When I place the uri link in browser, it's working but through program, error exception occuring.

Just I want to confirm that the uri address is valid and exists.

Placing my code below and the required details. Please help on this to resolve. Thanks.

uri path: https://www.parliament.uk/globalassets/documents/fair-society-healthy-lives-full-report.pdf

C#.net code:

 public static bool IsValidURL(string url)
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.AllowAutoRedirect = false;
                req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
                req.Timeout = 10000;
                req.UseDefaultCredentials = true;
                req.MaximumAutomaticRedirections = 5;
                req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36";
                req.CookieContainer = new CookieContainer();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    res.Close();
                    return true;
                }
                else
                {
                    res.Close();
                    return false;
                }
            }
            catch (WebException ex)
            {
                return false;
            }
        }
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 37,156 Reputation points
    2023-02-17T19:21:00.4+00:00

    It appears that the site is using cookies/javascript to combat bot's.

    I tried the site with Powershell's Invoke-Webrequest and also got the 403. Then I tried curl.exe noticed these comments in the returned data.

    User's image

    In Chrome I cleared my browsing data and got this.

    User's image

    It appears to be "working as designed".


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.