6

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");
6
  • Must work fine. What your expected result? Commented Oct 24, 2013 at 22:10
  • the WebClient works fine, as You get an empty string this is probably a url mistake Commented Oct 24, 2013 at 22:13
  • My expected result is the source code from that webpage, but it gets me a empty string Commented Oct 24, 2013 at 22:14
  • Do you have a firewall or proxy of anything like that which could be causing an outbound connection to fail, or redirect you to another page? Commented Oct 25, 2013 at 0:49
  • No, I dont have a Firewall or antivirus or anything Commented Oct 25, 2013 at 2:23

1 Answer 1

12

Here's a quick test:

xaml:

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

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

using string source_url = "http://thepiratebay.sx/ will work, so my guess is that they might be filtering requests from non browsers maybe?
@Noctis your solution to add userAgent in header is worked for me absolutely.
@HaiderAliWajihi Yep, because you fool the pirate bay to think you're a browser :). Glad it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.