Error Domain=NSURLErrorDomain Code=-1017 \"The operation couldn’t be

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

I just started ios development and I'm trying to exchange data with my api. When I'm doing POST requests everything is going fine but when I'm trying to do a GET request I get the following error:

Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be completed. (NSURLErrorDomain error -1017.)" UserInfo=0x145a2c00 {NSErrorFailingURLStringKey=http://myAPI.com/, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://myAPI.com, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x145b21d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1017.)"}

Could someone explain what's going wrong and how I can fix it?

My request:

-(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:     } } 

回答1:

You have something wrong with your JSON parameters:

kCFURLErrorCannotParseResponse = -1017



回答2:

If you forget to mention HTTPMethod, the error can also take place, I faced the Problem "NSURLErrorDomain Code=-1017", somehow i forget to add line "request.HTTPMethod = "POST".

After adding the line, my code worked perfectly.



回答3:

This error occurs when you perform a GET request with the body set (setHTTPBody)



回答4:

For any other people who got error code -1017 -- I fixed it by manually setting my http headers in my http request. I think the server was having problems parsing the HTTP headers because instead of the string "Authorization" : "qii2nl32j2l3jel" my headers didn't have the quotes like this: Authorization : "1i2j12j". Good luck.

Something like this:

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]; 


回答5:

PLease check this link

Alamofire.request(method, "(URLString)" , parameters: parameters, headers: headers).responseJSON { response in } 

in post method you also add

parameters:parameters, encoding: .JSON 

(if you added encoding line to GET POST then error will come "cannot parse response")

2nd please check the header as well.

["authentication_token" : "sdas3tgfghret6grfgher4y"] 

then Solved this issue.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!