2013-10-24 118 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是我认为你是一个浏览器:)工作绝对。 –