C# WebClient下载字符串未返回任何内容

C# WebClient下载字符串未返回任何内容,c#,webclient,downloadstring,C#,Webclient,Downloadstring,我想从海盗湾的搜索查询中获取源代码,我的代码中有这个,但它没有返回任何内容: WebClient webpage = new WebClient(); string source= webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0"); 下面是一个快速测试: xaml: 您的URL将返回一个空字符串,另一侧的Google将提供您正在查找的源代码 编辑: 正如我所设想的,您需要像web浏览器一样进行

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

WebClient webpage = new WebClient();
string source=  webpage.DownloadString("http://thepiratebay.sx/search/documentary/0/99/0");
下面是一个快速测试:

xaml:

您的URL将返回一个空字符串,另一侧的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);
}

你必须好好工作。你的预期结果是什么?WebClient工作正常,因为你得到一个空字符串,这可能是一个url错误。我的预期结果是该网页的源代码,但它得到了一个空字符串。你是否有防火墙或类似的代理,可能会导致出站连接失败,或将你重定向到另一个网页?否,我没有防火墙、防病毒软件或任何使用
string source\u url="http://thepiratebay.sx/
会起作用,所以我猜他们可能会过滤来自非浏览器的请求?@Noctis你在标题中添加userAgent的解决方案对我来说绝对有效。@HaiderAliWajihi是的,因为你愚弄了海盗湾,让他们认为你是一个浏览器:)。很高兴这有帮助。
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.";
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);
}