2013-10-24 117 views
5

我想從海盜灣的搜索查詢的源代碼,我有這個在我的代碼,但它不返回任何東西:WebClient的DownloadString不返回任何東西

WebClient webpage = new WebClient(); 
string source= webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0"); 
+0

工作在這裏很好。 –

+0

必須正常工作。你的預期結果是什麼? –

+0

WebClient正常工作,因爲您得到一個空字符串,這可能是一個url錯誤 –

回答

8

下面是一個簡單的測試:

XAML:

<Label Content="{Binding ElementName=window_name, Path=SourceTest}"></Label> 
<Label Content="{Binding ElementName=window_name, Path=SourceTest2}"></Label> 

代碼:

string source_url = "http://thepiratebay.sx/search/documentary"; 

WebClient webpage = new WebClient(); 
SourceTest = webpage.DownloadString(source_url); 
if (SourceTest == "") 
SourceTest = "stream was empty."; 


source_url = "http://www.google.com"; 

webpage = new WebClient(); 
SourceTest2 = webpage.DownloadString(source_url); 
if (SourceTest2 == "") 
    SourceTest2 = "stream was empty."; 

你的URL會返回一個空字符串,谷歌的另一邊,會給你你正在尋找的來源。

編輯: 正如我認爲,你需要找出像一個網頁瀏覽器。這與您的查詢:

string source_url = "http://thepiratebay.sx/search/documentary/0/99/0"; 

using (var webpage = new WebClient()) 
{ 
    webpage.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; 
    SourceTest = webpage.DownloadString(source_url); 
} 
+0

但我不需要谷歌的源代碼:/ –

+0

使用'string source_url =「http:// thepiratebay.sx /'可以工作,所以我的猜測是他們可能會過濾來自非瀏覽器的請求? – Noctis

+0

@Noctis您的解決方案在頭部添加的userAgent是我認爲你是一個瀏覽器:)工作絕對。 –