c# - Reading response data from a web service -
i'm using this hook client application web service.
in addition, i'm looking @ msdn reading getresponse per first link.
here's code i've got far:
public actionresult index() { webrequest request = webrequest.create("http://localhost:49474/api/store/get"); request.method = "get"; webresponse response = request.getresponse(); stream stores = response.getresponsestream(); encoding encode = system.text.encoding.getencoding("utf-8"); streamreader sr = new streamreader(stores, encode); char[] read = new char[1024]; int count = sr.read(read, 0, 1024); list<store> storeslist = new list<store>(); while (count > 0) { // need read contents of response strem above instantiated list of stores. } }
my api delivers data this:
public httpresponsemessage get() { list<store> stores = db.stores.tolist(); httpresponsemessage response = request.createresponse(httpstatuscode.ok, stores); return response; }
frankly i'm not sure go next. msdn link writes string concerns are:
- how know how many characters need read @ time each record?
- how read data sent api can use in view?
you can use sr.readtoend()
whole response string. @ least you'll able set breakpoint , @ in debugger make sure you're getting expect. either write function parse result kind of class, or use 3rd-party library parse them if they're in standard format; i.e. if results json use json.net.
note can use built-in async operations if blocking concern (usually it's not simple client apps, still way learn).