我发布json时收到错误。
错误域= NSURLErrorDomain代码= -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];
答案 0 :(得分:7)
我也遇到了这个错误,但我通过添加一行来克服这一点
request.HTTPMethod = "POST"
用于Objective-C编程使用:
[request setHTTPMethod:@"POST"];
答案 1 :(得分:1)
我和Alamofire有同样的问题,错误导致了这个问题:
let encoding = Alamofire.ParameterEncoding.JSON
let (encodedRequest, _) = encoding.encode(URLRequest, parameters: params)
我的变量params是dict [:]。我改为参数:nil,它的工作。
答案 2 :(得分:0)
对于获得error code -1017
的任何其他人 - 我通过在我的http请求中手动设置我的http标头来修复它。我认为服务器在解析HTTP头时遇到问题,因为而不是字符串“Authorization”:“qii2nl32j2l3jel”我的标题没有这样的引号:授权:“1i2j12j”。祝你好运。
这样的事情:
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];
答案 3 :(得分:0)
在get请求中设置参数时出现此1017错误:
let headers = [
"Cookie": "",
]
Alamofire.request(urlString, parameters: ["token": token],encoding: JSONEncoding.default, headers: headers).responseJSON { ...
这没有错误:
let headers = [
"Cookie": "",
"Authorization" : "Token " + token
]
Alamofire.request(urlString, method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON {