首页 > 代码库 > [Windows Phone 8开发]使用HttpWebRequest和HttpWebResponse向服务器发送Json数据(POST方法)

[Windows Phone 8开发]使用HttpWebRequest和HttpWebResponse向服务器发送Json数据(POST方法)

          public string szJson = "";          byte[] json;          UploadClass up = new UploadClass();            public ValidatePage()          {              InitializeComponent();          }           private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)         {             //我做了一个实现上传某些关键的Json数据,返回服务器Appid的功能,类似新浪微博获取AccessToken,不过微博SDK已经封装好了上传功能,比我这个简单许多。             GetAppid();         }          void GetAppid()         {             up.device = "Luma1020";             up.devid = "abcdefghijklmn123456";             up.os = "8.0";             up.appid = "987654321asdfghjkl";              DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(UploadClass));              //序列化要发送的信息             using (MemoryStream ms = new MemoryStream())             {                 dc.WriteObject(ms, up);  //报错的原因是UploadClass的类名没有写public                 json = ms.ToArray();                 ms.Close(); //一定要关闭流                 szJson = Encoding.UTF8.GetString(json, 0, json.Length);  //服务器要求用UTF8,就在代码中转换成UTF8,要求用别的就转换成相应的格式。             }              //从这一步开始,准备向服务器发送数据。             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 void GetRequestCallback(IAsyncResult asyncResult)         {             HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;             using (Stream sm = request.EndGetRequestStream(asyncResult))             {                 sm.Write(json, 0, json.Length);             }             request.BeginGetResponse(GetResponseCallback, request);         }          void GetResponseCallback(IAsyncResult result)         {             Stream stream = null;             UploadClass uc = null;  //new 一个供返回Json数据的实例              HttpWebRequest req = (HttpWebRequest)result.AsyncState;             WebResponse webresponse = req.EndGetResponse(result);             stream = webresponse.GetResponseStream();             using (StreamReader reader = new StreamReader(stream))             {                 string contents = reader.ReadToEnd();                  uc = new UploadClass();                  using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(contents.ToString())))                 {                     DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(UploadClass));                     uc = (UploadClass)ds.ReadObject(ms);                 }             }              //这种回调函数中不能直接在函数体里写UI的语句,需要用Dispatcher去通知UI改变。             if (uc.code == "0")             {                 Dispatcher.BeginInvoke                     (() =>                     {                        //这些都是前台UI,下面都是例子:                         IDTextBlock.Text = "AppId:  " + up.id;                         DeviceTextBlock.Text = "Device:  " + up.device;                     }                     );             }              else             {                 Dispatcher.BeginInvoke(() =>                 {                  //和上面一样,根据自己的UI自行设置。                 });             }         }

 

 

我在做这个模块时也参考了许多网上的例子,不过和大多数人的通病一样,就是他能正常运行,我就不能运行,因此我也排除了各种BUG,比如上面注释里写道的一些细节。

 

在标题里我把几个关键词也写上了,便于朋友们搜索,希望这篇博客能帮助你们!