公司最近项目里需要用到post json对象(所谓的json对象我理解的就是一个json字符串),在发送的时候总是有违预期结果,代码如下:
// 生成json数据
NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
[jsonDic setObject:@"HIHG022648" forKey:@"hotelDerbyPassport"];
[jsonDic setObject:@"JS" forKey:@"username"];
NSString *strPwd = @"123456";
NSString *strPwdMD5 = [strPwd md5HexDigest];
[jsonDic setObject:strPwdMD5 forKey:@"password"];
[jsonDic setObject:@"Jane" forKey:@"firstName"];
[jsonDic setObject:@"Smith" forKey:@"lastName"];
[jsonDic setObject:@"13333330000" forKey:@"mobilephone"];
[jsonDic setObject:@"1111111@qq.com" forKey:@"email"];
NSString *strJson = [jsonDic JSONString];
NSLog(@"%@",strJson);
NSURL *url = [NSURL URLWithString:@"http://218.xxx.xxx.85/interface/api/member/api_member_register"];
NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc] initWithURL:url];
NSMutableString *strPost = [[NSMutableString alloc] initWithString:strJson];
NSData *postData = [strPost dataUsingEncoding:NSUTF8StringEncoding];
[strPost release];
[self httpsEnginePost:postData postRequest:postRequest];
[postRequest release];
}
想起以前扒某网站的时候无论怎么搞,得到的数据都不是预期值,后来改了一个header的浏览器类型就ok了=。=,于是加上下面这段代码:
[postRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
well done。