0

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 ?

Matt Ball
  • 332,322
  • 92
  • 617
  • 683
user1306602
  • 217
  • 2
  • 4
  • 6

1 Answers1

0

This isn't a "character encoding" problem, per se. The string "2012-04-24%2002:24:13%20+0000" is simply URL-encoded.

I'm really not an Objective-C developer, but it looks like the problem stems from the Content-Type header you're setting:

[request setValue:@"application/x-www-form-urlencoded; ... ]

I'm not sure why you're using Content-Type: application/x-www-form-urlencoded but that definitely smells funny to me. Send JSON as application/json and that should fix the issue.

Community
  • 1
  • 1
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
  • Than you for your answer. If I switch the content type for "application/json", here is what I got: Error: Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x8bec380 {NSErrorFailingURLKey=http://192.168.0.215:90/MyWebService.asmx/AddUser, NSLocalizedDescription=Expected status code in (200-299), got 500} Any idea? – user1306602 Apr 24 '12 at 16:36
  • 1
    Basically, an HTTP response status code of 500 means that some sort of server error occurred. The web service code failed in some way. Can you debug it to see where it fails? – Matt Ball Apr 24 '12 at 18:07
  • The web service is not even hit. I mean the function is not even call, I really don't understand. Here is the code: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void AddUser(string user) { string filePath = @"C:\Resultat\test.txt"; File.AppendAllText(filePath, user + "\r\n"); // add the user } I have no idea of the problem. With "application/x-www-form-urlencoded" everything works fine, with "application/json" I got the error I post in my previous answer. Any idea? – user1306602 Apr 24 '12 at 19:47