Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to alll...
i have a doubt..
i have webservice link like:
http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo..
how to consume this webservice in c#...
i tried like this: rightclick on project and select Add webreference then Add web refrence window open.. and i copied the above link and enter,error came like that..

Internet Explorer cannot download citiesJSON from api.geonames.org and Internet Explorer was not able to open this internetsite.the Requested site is either unavailable or cannot be found.pls try again later.....

If any body knows for the above problem pls let me know..its very urgent for me and do the need full...
Posted

1 solution

I guess you are trying to Call REST based service which will return Json Data.

Firstly check for valid REST Service Url. If your REST Service url is correct then try calling service by below code.

string apiUrl = "YourFullServiceUrl";
Uri address = new Uri(apiUrl);

// Create the web request 
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(address) as System.Net.HttpWebRequest;
request.Method = "GET";
request.ContentType = "text/json";

using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
{
// Get the response stream 
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());

string strOutput = reader.ReadToEnd();
}
 
Share this answer
 
v2
Comments
komalilella 16-Jul-11 4:22am    
hi raiskazi..
basically i am new to this concept...if u know detail steps (step by step)pls let me know...and i don't know where to write the above code...pls do the need full.
komalilella 16-Jul-11 4:25am    
how to check for valid rest service url..pls let me know..
RaisKazi 16-Jul-11 4:38am    
I tried your url with my above code and it does returning data in Json format.

http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo
RaisKazi 16-Jul-11 4:33am    
REST Service is a HTTP url by calling which we get result either in XML/Json format. This type of Service does not requires adding "Web Reference". You can call above code on any of your event or in method. Have a look at below articles for more details on REST Services.

http://www.codeproject.com/KB/architecture/RESTWebServicesPart1.aspx
http://www.codeproject.com/KB/webservices/RestWebService.aspx?msg=3897933#xx3897933xx

Hope this helps.
RaisKazi 16-Jul-11 4:39am    
Please don't forget to "Accept Answer", if it helps you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900