2013-03-18 39 views
0

加載我用下面的代碼加載網頁內容:網頁無法System.Net.WebResponse

private string HttpGet(string uri) 
    { 
     WebRequest webRequest = WebRequest.Create(uri); 
     try 
     { 
      WebResponse webResponse = webRequest.GetResponse(); 
      StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"), false); 
      return sr.ReadToEnd().Trim(); 
     } 
     catch (WebException ex) 
     { 
      MessageBox.Show(ex.Message, "HttpGet: Response error", 
       MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     return null; 
    } 

在大多數情況下,我真正得到HTML內容。它適用於http; // stackoverflow.com,但是如果您嘗試加載http; // www.icetrade.by(我知道分號,只是不希望它們成爲鏈接),它可以在Web中正常工作瀏覽器,即使沒有啓用javascript,你會得到這樣的事情:

<br /> 
<b>Fatal error</b>: Class name must be a valid object or a string in <b>K:\hosting\icetrade.by\html\includes\module.class.php</b> on line <b>141</b><br /> 

什麼是這種行爲的原因,我怎樣才能得到真正的HTML內容?

回答

1

這絕對是一個服務器端錯誤。當某些標題(如UserAgent或Cookies)不存在時,可能本網站無法處理這種情況。

如果這是您控制的網站,請檢查module.class.php中的第141行發生了什麼

+0

非常感謝!你是對的,需要UserAgent頭。 – 2013-03-18 10:54:24