加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WebClient DownloadString没有返回任何内容

发布时间:2020-12-15 08:13:14 所属栏目:百科 来源:网络整理
导读:我想从Pirate Bay的Search查询中获取源代码,我在我的代码中有这个,但它没有返回任何内容: WebClient webpage = new WebClient();string source= webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0"); 解决方法 这是一个快速测试:
我想从Pirate Bay的Search查询中获取源代码,我在我的代码中有这个,但它没有返回任何内容:
WebClient webpage = new WebClient();
string source=  webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0");

解决方法

这是一个快速测试:

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.";

您的网址将返回一个空字符串,另一方面,Google将为您提供所需的来源.

编辑:
正如我所假设的,您需要识别Web浏览器.这适用于您的查询:

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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读