[Windows Phone 8 development] uses HttpWebRequest and HttpWebResponse to send JSON data to the server (POST method)

Source: Internet
Author: User

           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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.