做一个HTTP POST请求在WP7发送一个JSON文件 [英] Make an http post request to send a JSON file in WP7

查看:119
本文介绍了做一个HTTP POST请求在WP7发送一个JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的WP7设备发送一个 JSON文件到我的本地服务器。在iOS上我使用了 ASIHttpRequest 库,我所做的就是:

I would like to send a JSON file from my WP7 device to my local server. On iOS I used the ASIHttpRequest library and what I did was :

//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;
    }



我怎么能实现我的WP7应用程序相同的功能?

How could I implement the same functionality in my WP7 application?

我已经找到了什么到现在,我想我靠近:

What i ve found till now and i think i am close :

//Making a POST request using WebClient.

Function()
{    
  WebClient wc = new WebClient();

  var URI = new Uri("http://your_uri_goes_here");

  wc.Headers["Authorization"] = "Basic (here goes my credentials string which i 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 you the response against your POST request.
 }
  catch(Exception exc)         
  {             
     MessageBox.Show(exc.ToString());            
  }
}



我假设Data_to_be_Sent应该是jsonString在utf8编码?

I suppose that the "Data_to_be_Sent" should be the jsonString in utf8 encoding?

我注意到,在Data_To_Be_sent是一个字符串。不过,这应该是UTF8编码吧?所以,它应该是一个这是在UTF8格式字节数组。不过,我只能将一个字符串那里。我失去了什么吗?

I have noticed that the "Data_To_Be_sent" is a string. However this should be in UTF8 encoding right? So it should be a an array of bytes which are in UTF8 format. However i can only place a string there. What am i missing here?

推荐答案

Web客户端类有一个< A HREF =http://msdn.microsoft.com/en-us/library/system.net.webclient.encoding%28v=vs.95%29.aspx相对=nofollow>编码财产其中, UploadStringAsync DownloadStringAsync 方法使用。 。那里设置你的编码

The WebClient class has an Encoding property which the UploadStringAsync and DownloadStringAsync methods use. Set your encoding there.

wc.Encoding = Encoding.UTF8;

wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);   

wc.UploadStringAsync(URI,"POST","Data_To_Be_sent");    

这篇关于做一个HTTP POST请求在WP7发送一个JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆