我想发一个JSON file from my WP7 device to my local server
。在 iOS 上,我使用了该ASIHttpRequest
库,我所做的是:
//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 应用程序中实现相同的功能?
到目前为止我发现了什么,我想我已经接近了:
//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");
}
在哪里 :
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”应该是utf8编码的jsonString?
编辑
我注意到这"Data_To_Be_sent"
是一个字符串。然而这应该是UTF8编码吧?所以它应该是一个 UTF8 格式的字节数组。但是我只能在那里放置一个字符串。我在这里想念什么?