Can't get web html code: System.Net.WebException: 'The remote server returned an error: (403) Forbidden.

398 Views Asked by At

My problem is that I can't get specific website html code and I get this error: 'System.Net.WebException: 'The remote server returned an error: (403) Forbidden.'

My code is simple:

using (WebClient client = new WebClient())
        {
            string htmlCode = client.DownloadString("http://isbnsearch.org/isbn/");
            MessageBox.Show(htmlCode);
        }

When I try using other website like Google everything works perfectly, but with this website I can't reach it.

Is there any solution to fix this? Thanks

2

There are 2 best solutions below

2
Ashkan Mobayen Khiabani On

well as you dont have access to isbnsearch.org, so you can just catch the error and avoid your app break down, but can not solve it.

using (WebClient client = new WebClient())
        {
            try
            {
                 string htmlCode = client.DownloadString("http://isbnsearch.org/isbn/");
                 MessageBox.Show(htmlCode);
            }
            catch (Exception e)
            { 
                 MessageBox.Show(e.Message);
            }
        }
0
MCTG On

Found solution to get over this error:

        string url = "https://www.isbnsearch.org/";

        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = client.GetAsync(url).Result)
            {
                using (HttpContent content = response.Content)
                {
                    string result = content.ReadAsStringAsync().Result;
                    MessageBox.Show(result);
                }
            }
        }