c# - RestSharp on Windows Phone 8 got no response -
i tried connect api using restsharp. when try code:
var client = new restclient("http://api.com"); var request = new restrequest("api/login", method.post); client.authenticator = new httpbasicauthenticator("login", "pass"); request.addheader("accept", "*/*"); request.requestformat = dataformat.json; request.addbody(new { customer = new { email = "email", password = "pass" } }); var response = client.execute(request); console.writeline(response.content); console.readkey(); i got right response server
but when comes windows phone 8
var client = new restclient("http://api.com"); ; client.authenticator = new httpbasicauthenticator("login", "pass"); var request = new restrequest("api/login", method.post); request.addheader("accept", "*/*"); request.requestformat = dataformat.json; request.addbody(new { customer = new { email = "email", password = "pass" } }); client.executeasync(request, response => { lblstatus.text = response.content ; }); i got status code not found, server null. did wrong?
i think, must send request synchronyously. create class, looks this:
public static class restclientextensions { public static task<irestresponse> executetask(this irestclient client, restrequest request) { var taskcompletionsource = new taskcompletionsource<irestresponse>(); client.executeasync(request, (response, asynchandle) => { if (response.responsestatus == responsestatus.error) taskcompletionsource.setexception(response.errorexception); else taskcompletionsource.setresult(response); }); return taskcompletionsource.task; } } and now, able use this:
irestresponse restresponse = await app.client.executetask(request); after line can content of response, earlier:
lblstatus.text = response.content ; i hope you.
Comments
Post a Comment