2010-12-20 38 views
3

我有網址,如:
http://www.matweb.com/search/DataSheet.aspx?MatGUID=849e2916ab1541be9ff6a17b78f95c82matweb.com:如何獲取頁面的源?

我想使用此代碼從該頁面下載源代碼:

private static string urlTemplate = @"http://www.matweb.com/search/DataSheet.aspx?MatGUID="; 

static string GetSource(string guid) 
{ 
    try 
    { 
     Uri url = new Uri(urlTemplate + guid); 

     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
     webRequest.Method = "GET";    

     HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); 

     Stream responseStream = webResponse.GetResponseStream(); 
     StreamReader responseStreamReader = new StreamReader(responseStream); 
     String result = responseStreamReader.ReadToEnd(); 

     return result; 
    } 
    catch (Exception ex) 
    { 
     return null; 
    } 
} 

當我這樣做,我得到:

你不似乎啓用了Cookie。 MatWeb需要啓用Cookie。

好吧,我明白,所以我加了臺詞:

CookieContainer cc = new CookieContainer(); 
webRequest.CookieContainer = cc; 

我:

您的IP地址已經由於過度使用受到限制。當IP地址可能被公司中的許多人共享或通過互聯網服務提供商共享時,問題可能會更加複雜。很抱歉給您帶來不便。

我可以理解這一點,但當我嘗試使用Web瀏覽器訪問此頁面時,我沒有收到此消息。我能做些什麼來獲得源代碼?一些cookie或http頭文件?

回答

4

它可能不喜歡你的UserAgent。試試這個:

webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"; //maybe substitute your own in here 
+0

哦,謝謝!這樣可行 :)。順便說一句,我怎麼能得到我自己的UserAgent? – 2010-12-20 20:47:30

+0

要獲取您的瀏覽器發送的UserAgent,請嘗試以下網站:http://whatsmyuseragent.com/ – 2010-12-20 20:53:04

1

看起來你正在做一些公司不喜歡的事情,如果你有一個「過度使用」的迴應。

+0

他說只有在試圖運行他的程序時他纔會收到此消息。所以他的知識產權不是問題。 – 2010-12-20 20:42:05

+0

看起來像是用戶代理,請參閱上文。 – Broam 2010-12-20 22:35:24

0

您正在下載頁面的速度太快。

當您使用瀏覽器時,您可能每秒鐘最多達到一頁。使用應用程序,您可以每秒獲得幾頁,這可能是他們的Web服務器正在檢測的內容。因此過度使用。

+0

這不是真的,因爲即使我運行此功能一次,我也會收到此消息。所以太快的城鎮負荷不是問題。 – 2010-12-20 20:48:39