1
我已经浏览了关于此主题的多篇文章,但无法解决问题。 - 我在邮递员以下结果:客观c - 试图将“原始”“application/json”数据发布到服务器API
请求通过邮递员发送与以下原料 - 应用程序/ JSON体:收到
成功响应:
然而,当我尝试使用以下代码获得相同的工作时
NSDictionary *postContent = @{ @"interests" : @"[\"TECHNOLOGY\",\"COOKING\",\"FINANCE\",\"SPORTS\",\"PHOTOGRAPHY\",\"FASHION\"]", @"user" : @"0", @"deviceId" : @"abcd", @"country_code" : @"IN" };
NSError *error = nil;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:postContent options:0 error:&error];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postdata length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:HOME_VIEWS]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postdata];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
}] resume];
我总是零字节的数据得到以下错误:
{ status code: 404, headers {
Connection = close;
"Content-Type" = "application/json; charset=utf-8";
Date = "Thu, 15 Dec 2016 11:51:29 GMT";
"Set-Cookie" = "JSESSIONID=B0FE830D979F7BFAD0DDA67AEB9E2999.views; Path=/proliphiq";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "Servlet 2.5; JBoss-5.0/JBossWeb-2.1";
} }
404通常意味着未找到URL。 你确定'[NSURL URLWithString:HOME_VIEWS]'指向一个有效的网址吗?三重检查一个。 – Ramon
嗨拉蒙 - 是的,这是正确的。我打印了URL并在浏览器中使用,并且工作正常。 POSTman也使用相同的URL –