Webclient request returns empty

Asked

Viewed 507 times

1

In my program I need to get the contents of my site, but the return of the downloadString method of the Webclient object returns null. The most intriguing is that there is no exception, the status code is 200, the request is performed perfectly but the url does not return anything.

WebClient wc = new WebClient();
String teste = wc.DownloadString("http://www.wiplay.com.br");

My website http://www.wiplay.com.br

  • When it comes to debugging HTTP problems, the Fiddler is your best friend.

2 answers

1


You have to set the request headers before making the request:

WebClient wc = new WebClient();
wc.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
wc.Headers["Accept-Language"] = "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4";
String teste = wc.DownloadString("http://www.wiplay.com.br/");

I just tested this code and it works here.

When you don’t know which headers and how to set the headers, use Fiddler and see what the browser is sending to the site server... then it’s just a matter of copying the headers' values until it works.

  • Miguel Angelo, Thank you very much your reply was very constructive will exactly that, for sure a server configuration requiring more headers.

0

Try using a HttpClient in place of WebClient:

HttpClient client = new HttpClient();
Uri uri = new Uri("http://www.wiplay.com.br");
HttpResponseMessage response = await client.GetAsync(uri);

if (response.IsSuccessStatusCode)
{
    var teste = response.Content.ReadAsStringAsync().Result;
} else {
    // Lógica para tratamento de status diferente de 200
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.