错误域= NSURLErrorDomain代码= -1017"无法解析响应"

use*_*124 7 objective-c ios ios8

我在发布json时遇到错误.

Error Domain = NSURLErrorDomain Code = -1017"无法解析响应"UserInfo = 0x7f89dac01a40 {NSUnderlyingError = 0x7f89e0277a20"无法解析响应",NSErrorFailingURLStringKey = http://test-onboard.qlc.in/FieldSense/authenticate,NSErrorFailingURLKey = http:// test-onboard.qlc.in/FieldSense/authenticate,_kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1,NSLocalizedDescription =无法解析响应}

这是我的代码:

NSDictionary *dictionaryData=[[NSDictionary alloc]initWithObjectsAndKeys:self.txtUsername.text, @"userEmailAddress", self.txtPassword.text, @"password",nil];            
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryData options:kNilOptions error:&error];

[request setURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
Run Code Online (Sandbox Code Playgroud)

pri*_*tam 7

我也得到了这个错误,但我通过添加一行来克服这一点

request.HTTPMethod = "POST"
Run Code Online (Sandbox Code Playgroud)

对于Objective-C编程使用这个:

[request setHTTPMethod:@"POST"];
Run Code Online (Sandbox Code Playgroud)


aBi*_*l17 0

对于任何其他人error code -1017- 我通过在 http 请求中手动设置 http 标头来修复它。我认为服务器在解析 HTTP 标头时遇到问题,因为我的标头没有像这样的引号:Authorization:“1i2j12j”,而不是字符串“Authorization”:“qii2nl32j2l3jel”。祝你好运。

像这样的东西:

NSDictionary* newRequestHTTPHeader = [[NSMutableDictionary alloc] init];
[newRequestHTTPHeader setValue:authValue forKey:@"\"Authorization\""];
[newRequestHTTPHeader setValue:contentLengthVal forKey:@"\"Content-Length\""];
[newRequestHTTPHeader setValue:contentMD5Val forKey:@"\"Content-MD5\""];
[newRequestHTTPHeader setValue:contentTypeVal forKey:@"\"Content-Type\""];
[newRequestHTTPHeader setValue:dateVal forKey:@"\"Date\""];
[newRequestHTTPHeader setValue:hostVal forKey:@"\"Host\""];
[newRequestHTTPHeader setValue:publicValue forKey:@"\"public-read-write\""];

//the proper request is built with the new http headers.
NSMutableURLRequest* request2 = [[NSMutableURLRequest alloc] initWithURL:request.URL];
[request2 setAllHTTPHeaderFields:newRequestHTTPHeader];
[request2 setHTTPMethod:request.HTTPMethod];
Run Code Online (Sandbox Code Playgroud)