7

In my program I need to get the content of my site, however the return of DownloadString method of the webclient object returns null, however the most intriguing is that there is no exception. the status code is 200, the request is made ​​perfectly, but the url returns an empty string.

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

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

2
  • 3
    That's an empty string, not null. The site probably wants more headers. Commented Mar 6, 2014 at 17:41
  • is there a way to know which headers is mandatory? Commented Mar 6, 2014 at 17:45

2 Answers 2

5

Seems like your website requires the user agent header to be set in order to respond.

Add the following before your call the DownloadString method:

wc.Headers.Add(HttpRequestHeader.UserAgent, "your useragent string");
Sign up to request clarification or add additional context in comments.

4 Comments

Yup looks like that works... but a poor site design, IMHO. No default page?
Some websites use this method to avoid crawlers mainly. So a poor security feature I'd say.
coolmine Thanks, was exactly this. I am the site administrator but not of the server, the server is shared, is there anything I can do about that poor security feature that you say?
Hard to say, depends how it is implemented, since this can be done with various ways, from the PHP code to the .htaccess file. So the first step would be to find out which method is used.
0

In my case use of HttpClient and WebClient resulted in empty string despite status code 200 no matter what headers I set. If somebody still suffers to this problem,using RestSharp like finally returned expected response body.

var dataString = JObject.FromObject(anonymousObject).ToString();
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddParameter("application/json", dataString, ParameterType.RequestBody);

var response = client.Post(request);

Comments

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.