错误域= NSURLErrorDomain代码= -1017“操作不能
我刚开始ios开发,我试图与我的api交换数据。 当我做POST请求时,一切都很好,但是当我试图做GET请求时,我得到以下错误:
错误域= NSURLErrorDomain代码= -1017“该操作无法完成。(NSURLErrorDomain错误-1017。)”UserInfo = 0x145a2c00 {NSErrorFailingURLStringKey = http://myAPI.com/,_kCFStreamErrorCodeKey = -1,NSErrorFailingURLKey = http:/ / myAPI.com ,_kCFStreamErrorDomainKey = 4,NSUnderlyingError = 0x145b21d0“操作无法完成(kCFErrorDomainCFNetwork错误-1017。)”}
有人可以解释发生了什么问题,我该如何解决?
我的请求:
-(void)hitApiWithURL:(NSString*)url HTTPMethod:(NSString*)HTTPMethod params:(NSDictionary*)params successBlock:(successTypeBlock)success failureBlock:(errorTypeBlock)failure{ NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; [sessionConfig setHTTPAdditionalHeaders:@{@"Content-type": @"application/json"}]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; [request setHTTPMethod:HTTPMethod]; // The body NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error]; [request setHTTPBody:jsonData]; NSURLSessionDataTask *dataTaks = [session dataTaskWithRequest:request]; [dataTaks resume]; NSLog(@"dataTask started"); } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { if (error) { //Gives my error } else { // do something: } }
你的JSON参数有问题:
kCFURLErrorCannotParseResponse = -1017
如果你忘了提到HTTPMethod,错误也可能发生,我面临的问题“NSURLErrorDomain代码= -1017”,不知何故,我忘了添加行“request.HTTPMethod =”POST“。
添加行后,我的代码完美工作。
当您使用正文集( setHTTPBody
)执行GET
请求时,会发生此错误
对于任何其他人得到错误代码-1017
– 我通过手动设置我的http请求中的http头解决了它。 我认为服务器parsingHTTP标题时出现问题,因为不是string"Authorization" : "qii2nl32j2l3jel"
我的标题没有像这样的引号: Authorization : "1i2j12j"
。 祝你好运。
像这样的东西:
NSMutableDictionary* 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];
请检查这个链接
Alamofire.request(method, "(URLString)" , parameters: parameters, headers: headers).responseJSON { response in }
在后期的方法,你也添加
parameters:parameters, encoding: .JSON
(如果你添加了编码行GET POST,那么错误将会出现“无法parsing响应”)
第二,请检查标题。
["authentication_token" : "sdas3tgfghret6grfgher4y"]
那么解决了这个问题。