在iOS中,我正在构建一个http请求,如下所示:
NSURL* url = [ NSURL URLWithString:[ NSString stringWithFormat: @"%@%@", [ HPSWebService getBaseURL ], @"Sync/PushObject"] ];
// 'request' is defined within the base class and is set here to the class-specific server url
request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString* jsonRequest = [NSString stringWithFormat: @"{\"collection\":\"responses\",\"id\":\"%@\",\"objectjson\":%@}",response.id,response.json];
NSLog(@"HPSPushResponsesWebService jsonRequest = %@",jsonRequest);
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:self.contentEncoding forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSLog输出如下:
2012-10-23 15:45:51.543 HPS [12523:707] HPSPushResponsesWebService jsonRequest = {“collection”:“respond”,“id”:“ZBZiaa43QciMSlMCp0Vtyw ==”,“objectjson”:{“_ id”: “{\”$ binary \“:\”ZBZiaa43QciMSlMCp0Vtyw == \“,\”$ type \“:\”03 \“}”,“_ user”:“Eddie Freshman”,“_ lastmodifieddatelocalutc”:“2012-10- 23 14:45:39“,”Fav Car“:[”其他:Me√'zoom“],”_ formid“:”{\“$ binary \”:\“OCDI9VNGStmN8GbOLSevtA == \”,\“$ type \ “:\”03 \“}”,“需要跟进?”:[],“_ contactid”:“{\”$ binary \“:\”+ 3144XhxQzCNzWKfqXCHyg == \“,\”$ type \“: \“03 \”}“}}
在JSON数据包含特殊字符(例如变音符号)之前,一切正常。当发生这种情况时,内容长度实际上比它应该少1个字符。在示例中,umlat(e)char已转换为√'(靠近Fav Car string)
我应如何对此进行编码,以便在http请求中正确设置内容长度?
由于