WebClient request returns empty

1

In my program I need to get content from my website, 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 perfectly executed but the url does not return anything.

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

My site link

    
asked by anonymous 06.03.2014 / 18:43

2 answers

1

You have to define 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 do not know which headers and how to set the headers, use the fiddler and see what the browser is sending to the site server ... then it's just a matter of copying the headers values up and running.

    
06.03.2014 / 19:08
0

Try using HttpClient instead 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
}
    
06.03.2014 / 18:51