簡體   English   中英

無法獲取Web HTML代碼:System.Net.WebException:'遠程服務器返回錯誤:(403)禁止。

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

我的問題是我無法獲取特定的網站html代碼,並且收到以下錯誤:'System.Net.WebException:'遠程服務器返回了錯誤:(403)禁止。

我的代碼很簡單:

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

當我嘗試使用其他網站(例如Google)時,一切運行正常,但是使用此網站卻無法訪問。

有解決此問題的解決方案嗎? 謝謝

以及您無權訪問isbnsearch.org,因此您可以捕獲錯誤並避免應用崩潰,但是無法解決。

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

找到解決此錯誤的解決方案:

        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);
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM