c# - Get Response from HttpWebRequest on Windows Phone 8 -
i'm trying webrequest site windows phone application. vry important me response server. here code:
uri requesturi = new uri(string.format("http://localhost:8099/hello/{0}", metodo)); httpwebrequest httpwebrequest = (httpwebrequest)webrequest.create(requesturi); httpwebrequest.contenttype = "application/xml; charset=utf-8"; httpwebrequest.method = "post"; using (var stream = await task.factory.fromasync<stream>(httpwebrequest.begingetrequeststream, httpwebrequest.endgetrequeststream, null)) { string xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/serialization/\">ahri</string>"; byte[] xmlasbytes = encoding.utf8.getbytes(xml); await stream.writeasync(xmlasbytes, 0, xmlasbytes.length); }
unfortunatelly, have no idea of how response server. have idea?
thanks in advance.
thanks @max found solution , wanted share above. here how code looks like:
string xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/serialization/\">claor</string>"; uri requesturi = new uri(string.format("http://localhost:8099/hello/{0}", metodo)); string responsefromserver = "no response"; httpwebrequest httpwebrequest = httpwebrequest.create(requesturi) httpwebrequest; httpwebrequest.contenttype = "application/xml; charset=utf-8"; httpwebrequest.method = "post"; using (stream requeststream = await httpwebrequest.getrequeststreamasync()) { byte[] xmlasbytes = encoding.utf8.getbytes(xml); await requeststream.writeasync(xmlasbytes, 0, xmlasbytes.length); } webresponse webresponse = await httpwebrequest.getresponseasync(); using (var reader = new streamreader(webresponse.getresponsestream())) { responsefromserver = reader.readtoend(); }
i hope in future.
this common question people new in windows phone app development. there several sites gives tutorials same want give small answer here.
in windows phone 8 xaml/runtime can using httpwebrequest or webclient.
basically webclient wraper around httpwebrequest.
if have small request make user httpwebrequest. goes this
httpwebrequest request = httpwebrequest.create(requesturi) httpwebrequest; webresponse response = await request.getresponseasync(); using (var reader = new streamreader(response.getresponsestream())) { string responsecontent = reader.readtoend(); // content. convert xml, json or anything. }
although request , see want post request, have modify few steps achieve that.
if want windows phone tutorials, can go here. writes awesome tuts.
Comments
Post a Comment