在我的 iOS 应用程序中,我像这样使用 NSURLConnection 将 json 字符串发布到服务器。
post_string = @"{"function":"getHuddleDetails", "parameters": {"user_id": "167","location_id": "71","huddle_id": "328","checkin_id": "1287"},"token":""}"
-(void)myServerRequests:(NSString *)post_string{
NSData *postData = [post_string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.myServerURl.com/jsonpost"]];
[request setTimeoutInterval:100];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
webData = [[NSMutableData alloc]init];
}
}
并使用以下代码使用 NSURLSession 发布multipart/form-data(不是 JSON)
-(void)myFunction:(NSString *)user_name paswd:(NSString *)pass_word {
NSString *boundary = [self boundaryString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.myServerURl.com/formpost"]];
[request setHTTPMethod:@"POST"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n%@", user_name] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n%@", pass_word] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:postbody completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSAssert(!error, @"%s: uploadTaskWithRequest error: %@", __FUNCTION__, error);
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
}];
[task resume];
}
这在 NSURLSession 上工作得很好,但是当我尝试发布 json 字符串(将其转换为 NSDATA 并使用 NSURLSession 发布)时,它不起作用。
为什么会这样? 提前致谢
最佳答案
如果您想将 JSON 发送到服务器,您应该将内容类型相应地设置为 application/json
而您的内容实际上就是这样:JSON 编码最好是 UTF-8。
在你的方法中 myServerRequests:
你设置内容类型 application/x-www-form-urlencoded
- 但你没有正确设置相应的内容。如何做到这一点可以在这里阅读:URL-encoded form data .此外,指定字符集参数根本不起作用。
如果您想为 application/x-www-form-urlencoded
发送字符串参数内容类型,您也不应该使用有损转换。而是使用 UTF-8。注意 NSString
s length
返回 UTF-16 代码点的数量 - 这与 UTF-8 编码字符串的字节数不同。
回顾:
不要使用 application/x-www-form-urlencoded 内容类型。相反:
Content-Type = application/json
Content-Length = <length in bytes of the UTF-8 encoded JSON>
当您解决这些问题时,请求应该没问题。
当使用多部分/表单数据请求时,我强烈建议使用网络库。手动执行此操作太容易出错,您需要阅读至少 300 个 RFC 才能知道您是否正确执行此操作;)
关于ios - 在 NSURLSession 中 POST json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27022520/