C# WebClient DownloadString 不返回任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19577586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebClient DownloadString not returning anything
提问by Marcelo Cantú
I want to get the source code from a Search query of Pirate Bay, I have this in my code but it doesn't return anything:
我想从海盗湾的搜索查询中获取源代码,我的代码中有这个,但它没有返回任何内容:
WebClient webpage = new WebClient();
string source= webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0");
采纳答案by Noctis
Here's a quick test:
这是一个快速测试:
xaml:
xml:
<Label Content="{Binding ElementName=window_name, Path=SourceTest}"></Label>
<Label Content="{Binding ElementName=window_name, Path=SourceTest2}"></Label>
Code:
代码:
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.";
Your URL will return an empty string, Google on the other side, will give you the source you're looking for.
您的 URL 将返回一个空字符串,另一方面,Google 将为您提供您正在寻找的来源。
Edit :As I assumed, you need to identify like a web browser. This works with your query:
编辑:正如我所假设的,您需要像网络浏览器一样进行识别。这适用于您的查询:
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);
}