How do I make calls to a REST api using c#? -
this code have far:
using system; using system.collections.generic; using system.linq; using system.text; using system; using system.net.http; using system.web; using system.net; using system.io; namespace consoleprogram { public class class1 { private const string url = "https://sub.domain.com/objects.json?api_key=123"; private const string data = @"{""object"":{""name"":""name""}}"; static void main(string[] args) { class1.createobject(); } private static void createobject() { httpwebrequest request = (httpwebrequest)webrequest.create(url); request.method = "post"; request.contenttype = "application/json"; request.contentlength = data.length; streamwriter requestwriter = new streamwriter(request.getrequeststream(), system.text.encoding.ascii); requestwriter.write(data); requestwriter.close(); try { webresponse webresponse = request.getresponse(); stream webstream = webresponse.getresponsestream(); streamreader responsereader = new streamreader(webstream); string response = responsereader.readtoend(); console.out.writeline(response); responsereader.close(); } catch (exception e) { console.out.writeline("-----------------"); console.out.writeline(e.message); } } } }
the problem think exception block being triggered (because when remove try-catch, server error (500) message. don't see console.out lines put in catch block.
my console:
the thread 'vshost.notifyload' (0x1a20) has exited code 0 (0x0). thread '<no name>' (0x1988) has exited code 0 (0x0). thread 'vshost.loadreference' (0x1710) has exited code 0 (0x0). 'consoleapplication1.vshost.exe' (managed (v4.0.30319)): loaded 'c:\users\l. preston sego iii\documents\visual studio 11\projects\consoleapplication1\consoleapplication1\bin\debug\consoleapplication1.exe', symbols loaded. 'consoleapplication1.vshost.exe' (managed (v4.0.30319)): loaded 'c:\windows\microsoft.net\assembly\gac_msil\system.configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\system.configuration.dll', skipped loading symbols. module optimized , debugger option 'just code' enabled. first chance exception of type 'system.net.webexception' occurred in system.dll thread 'vshost.runparkingwindow' (0x184c) has exited code 0 (0x0). thread '<no name>' (0x1810) has exited code 0 (0x0). program '[2780] consoleapplication1.vshost.exe: program trace' has exited code 0 (0x0). program '[2780] consoleapplication1.vshost.exe: managed (v4.0.30319)' has exited code 0 (0x0).
i'm using visual studio 2011 beta, , .net 4.5 beta.
the asp.net web api has replaced wcf web api mentioned.
i thought i'd post updated answer since of these responses 2012, , thread 1 of top results when doing google search "call restful service c#".
current guidance microsoft use microsoft asp.net web api client libraries consume restful service. available nuget package, microsoft.aspnet.webapi.client.
here's how example when implemented using asp.net web api client library:
using system; using system.collections.generic; using system.net.http; using system.net.http.headers; namespace consoleprogram { public class dataobject { public string name { get; set; } } public class class1 { private const string url = "https://sub.domain.com/objects.json"; private string urlparameters = "?api_key=123"; static void main(string[] args) { httpclient client = new httpclient(); client.baseaddress = new uri(url); // add accept header json format. client.defaultrequestheaders.accept.add( new mediatypewithqualityheadervalue("application/json")); // list data response. httpresponsemessage response = client.getasync(urlparameters).result; // blocking call! if (response.issuccessstatuscode) { // parse response body. blocking! var dataobjects = response.content.readasasync<ienumerable<dataobject>>().result; foreach (var d in dataobjects) { console.writeline("{0}", d.name); } } else { console.writeline("{0} ({1})", (int)response.statuscode, response.reasonphrase); } } } }
for more details, including other examples, go here: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
this blog post may useful: http://johnnycode.com/2012/02/23/consuming-your-own-asp-net-web-api-rest-service/
Comments
Post a Comment