Public stringSzjson =""; byte[] JSON; Uploadclass up=NewUploadclass (); PublicValidatepage () {InitializeComponent (); } Private voidPhoneapplicationpage_loaded (Objectsender, RoutedEventArgs e) { //I made an implementation to upload some key JSON data, back to the server AppID function, similar to Sina Weibo to get Accesstoken, but the microblog SDK has encapsulated the upload function, much simpler than mine. Getappid (); } voidGetappid () {Up.device="Luma1020"; Up.devid="abcdefghijklmn123456"; Up.os="8.0"; Up.appid="987654321ASDFGHJKL"; DataContractJsonSerializer DC=NewDataContractJsonSerializer (typeof(Uploadclass)); //serialize the information to be sent using(MemoryStream ms =NewMemoryStream ()) {DC. WriteObject (MS, up); //the reason for the error is that the Uploadclass class name is not written publicJSON =Ms. ToArray (); Ms. Close (); //Be sure to close the streamSzjson = Encoding.UTF8.GetString (JSON,0Json. Length);//server requirements with UTF8, in the code to switch to UTF8, require the conversion to the corresponding format. } //starting with this step, you are ready to send data to the server. HttpWebRequest request = (HttpWebRequest) webrequest.create ("http://api.xxxx.com/Api/xxxxx"); Request. Method="POST"; Request. ContentType="Application/json"; Request. ContentLength=JSON. Length; IAsyncResult result=(IAsyncResult) request. BeginGetRequestStream (getrequestcallback, request); } Private voidGetrequestcallback (IAsyncResult asyncResult) {HttpWebRequest Request=(HttpWebRequest) asyncresult.asyncstate; using(Stream SM =request. EndGetRequestStream (AsyncResult)) {Sm. Write (JSON,0, JSON. Length); } request. BeginGetResponse (getresponsecallback, request); } voidgetresponsecallback (IAsyncResult result) {Stream stream=NULL; Uploadclass UC=NULL;//new An instance for returning JSON dataHttpWebRequest req=(HttpWebRequest) result. asyncstate; WebResponse WebResponse=req. EndGetResponse (result); Stream=WebResponse. GetResponseStream (); using(StreamReader reader =NewStreamReader (Stream)) { stringContents =Reader. ReadToEnd (); UC=NewUploadclass (); using(MemoryStream ms =NewMemoryStream (Encoding.UTF8.GetBytes (contents. ToString ()))) {DataContractJsonSerializer DS=NewDataContractJsonSerializer (typeof(Uploadclass)); UC=(Uploadclass) ds. ReadObject (MS); } } //This callback function can not directly write the UI in the function body statement, need to use dispatcher to notify the UI changes. if(Uc.code = ="0") {Dispatcher.begininvoke ()= { //These are the front-desk UI, here are examples:Idtextblock.text ="AppId:"+up.id; Devicetextblock.text="Device:"+Up.device; } ); } Else{dispatcher.begininvoke ()= { //as above, set your own UI according to your own. }); } }
I also refer to a lot of online examples when I do this module, but as with most people, it is that he can run normally, I can't run, so I also ruled out a variety of bugs, such as some of the details in the comments above.
In the title I put a few keywords also written, easy to search friends, I hope this blog can help you!