可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.