Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to get response from HttpWebRequest

I have following code written on mono (Mono on ubuntu)

string URI = "http://www.google.com/webmasters/tools/feeds/http%3A%2F%2Fwww%2Ekarkala%2Ein%2F/keywords/?access_token=ya29.ABCDEFGI7bzJmlLWtk290M-PkNx20ej9p6a0sxoaxFPe_7qypXuW7Q";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
request.Headers.Add("GData-Version", "2");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

The code is part of getting response from Google webmaster API. But I get the following error while trying to get the response

The remote server returned an error: (400) Bad Request.

System at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0 at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0

If I copy-paste the same URI on my browser, I am able to see the xml response.

like image 800
Vijay Karla Avatar asked Dec 18 '25 15:12

Vijay Karla


1 Answers

You have to set the request content type:

request.ContentType = "text/xml";

Otherwise, the remote server won't know what to do with your request.

like image 63
aevitas Avatar answered Dec 21 '25 03:12

aevitas