I'm consuming a web service (who return me JSON format) in my iPhone app. Everything works great, I can call my web service with a parameter (in the example below the parameter is a string which contains JSON format) and get the JSON answer from my web service. My problem is that my web service receive my parameter with escaped characters (UTF8). Here is the code:
// 1 - iOS app
NSString *parameters = @"&user={"Name":"aName","Date":"2012-04-24 02:24:13 +0000"}";
NSData *postData = [parameters dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *urlString = @"http://myWebService:80/AddUser";
NSURL *url = [NSURL URLWithString:urlString];
unsigned long long postLength = postData.length;
NSString *contentLength = [NSString stringWithFormat:@"%ull",postLength];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
// send the request...
------------------------------------------------------------------------------
// 2 - Web service
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void AddUser(string user)
{
Dictionary<string, string> userData = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(user);
string name = userData["Name"]; // OK here, name == "aName"
DateTime newDateTime = DateTime.Parse(userData["Date"]); // ERROR here because userData["Date"] returns "2012-04-24%2002:24:13%20+0000" so Parse method crash
// ... return the JSON answer
}
First, is that normal that the parameter received still contains the escaped characters? If yes, how can I turn the string "user":
{"Name":"aName","Date":"2012-04-24%2002:24:13%20+0000"}
into that:
{"Name":"aName","Date":"2012-04-24 02:24:13 +0000"}
Otherwise, did I do something wrong when I build my request on my iOS app ?