首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebClient DownloadString不返回任何内容

WebClient DownloadString不返回任何内容
EN

Stack Overflow用户
提问于 2013-10-25 06:05:53
回答 1查看 25.3K关注 0票数 6

我想从海盗湾的一个搜索查询中获取源代码,我的代码中有以下内容,但它没有返回任何内容:

代码语言:javascript
复制
WebClient webpage = new WebClient();
string source=  webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-25 09:03:35

下面是一个快速测试:

xaml:

代码语言:javascript
复制
<Label Content="{Binding ElementName=window_name, Path=SourceTest}"></Label>
<Label Content="{Binding ElementName=window_name, Path=SourceTest2}"></Label>

代码:

代码语言:javascript
复制
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将返回一个空字符串,而另一边的Google将为你提供你正在寻找的资源。

编辑:正如我所假设的,你需要像web浏览器一样识别。这适用于您的查询:

代码语言:javascript
复制
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);
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19577586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档