Ios 在NSURLSession中发布json字符串
在我的iOS应用程序中,我使用NSURLConnection向服务器发送json字符串,如下所示Ios 在NSURLSession中发布json字符串,ios,objective-c,iphone,nsurlconnection,nsurlsession,Ios,Objective C,Iphone,Nsurlconnection,Nsurlsession,在我的iOS应用程序中,我使用NSURLConnection向服务器发送json字符串,如下所示 post_string = @"{"function":"getHuddleDetails", "parameters": {"user_id": "167","location_id": "71","huddle_id": "328","checkin_id": "1287"},"token":""}" -(void)myServerRequests:(NSString *)pos
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发布多部分/表单数据(非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
——但没有正确设置相应的内容。如何做到这一点可以在此处阅读:。此外,指定字符集参数根本没有效果
如果要为应用程序/x-www-form-urlencoded
内容类型发送字符串参数,也不应使用有损转换。而是使用UTF-8。请注意,NSString
slength
返回UTF-16代码点的数量-这与UTF-8编码字符串的字节数不同
重述:
不要使用application/x-www-form-urlencoded内容类型。相反:
Content-Type=application/json
内容长度=
当您解决这些问题时,请求应该是确定的
当使用多部分/表单数据请求时,我强烈建议使用网络库。手工操作太容易出错,您需要阅读至少300份RFC才能知道您的操作是否正确;) 您必须比“不工作”做得更好。报告的错误、您是否获得了不正确的数据、您是否跟踪了您在服务器上获得或未获得的内容?还有很多事情要告诉我们。