c# - Make an http post request to send a JSON file in WP7 -


i send json file wp7 device local server. on ios used asihttprequest library , did :

//send json file , using asihttpclass nsurl *url = [nsurl urlwithstring:urlstr]; asihttprequest *request = [asihttprequest requestwithurl:url]; request.timeoutseconds = time_out_seconds; [request setrequestmethod:@"put"];  nsstring *credentials= [self encodecredentials]; [request addrequestheader:@"authorization" value:[[nsstring alloc] initwithformat:@"basic %@",credentials]]; [request addrequestheader:@"content-type" value:@"application/json; charset=utf-8"];          [request appendpostdata:[jsonstring datausingencoding:nsutf8stringencoding]]; [request startsynchronous];  if([request responsestatuscode]==200){    return true; } else {      return false;     } 

how implement same functionality in wp7 application?

what ve found till , think close :

//making post request using webclient.  function() {       webclient wc = new webclient();    var uri = new uri("http://your_uri_goes_here");    wc.headers["authorization"] = "basic (here goes credentials string have)";    wc.headers["content-type"] = "application/json; charset=utf-8";   wc.uploadstringcompleted += new uploadstringcompletedeventhandler(wc_uploadstringcompleted);   wc_cart_session.uploadstringasync(uri,"post","data_to_be_sent");      } 

where :

void wc__uploadstringcompleted(object sender, uploadstringcompletedeventargs e) {     try               {                messagebox.show(e.result);  //e.result fetches response against post request.  }   catch(exception exc)            {                   messagebox.show(exc.tostring());               } } 

i suppose "data_to_be_sent" should jsonstring in utf8 encoding?

edit


i have noticed "data_to_be_sent" string. should in utf8 encoding right? should array of bytes in utf8 format. can place string there. missing here?

the webclient class has encoding property uploadstringasync , downloadstringasync methods use. set encoding there.

wc.encoding = encoding.utf8;  wc.uploadstringcompleted += new uploadstringcompletedeventhandler(wc_uploadstringcompleted);     wc.uploadstringasync(uri,"post","data_to_be_sent");     

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -