c# - How to make a GET RESTful Request -
i need include curl -h 'context-type:application/json' within url not sure how this, server responce far 404, appreciated,
private string requestvehicledata() { string make = ""; string postcode = ""; string registration = (string)(session["regno"]); make = txtmake.text; postcode = txtpostcode.text; //make request var httpwebrequest = (httpwebrequest)webrequest.create(string.format("https://www.check-mot.service.gov.uk/api/v1/mot-history/{0}/{1}/", registration, make)); httpwebrequest.contenttype = "application/json"; httpwebrequest.method = "get"; //get response var httpresponse = (httpwebresponse)httpwebrequest.getresponse(); using (var streamreader = new streamreader(httpresponse.getresponsestream())) { var result = streamreader.readtoend(); return result; } }
try httpclient. here example copied msdn: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client:
using (var client = new httpclient()) { client.baseaddress = new uri("http://localhost:9000/"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); // new code: httpresponsemessage response = await client.getasync("api/products/1"); if (response.issuccessstatuscode) { product product = await response.content.readasasync>product>(); console.writeline("{0}\t${1}\t{2}", product.name, product.price, product.category); } }
Comments
Post a Comment