简体   繁体   English

错误域=NSURLErrorDomain 代码=-1017 “操作不能

[英]Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be

I just started ios development and I'm trying to exchange data with my api.我刚开始 ios 开发,我正在尝试与我的 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:当我执行 POST 请求时,一切正常,但是当我尝试执行 GET 请求时,出现以下错误:

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.)"} Error Domain=NSURLErrorDomain Code=-1017 “操作无法完成。(NSURLErrorDomain 错误 -1017。)” UserInfo=0x145a2c00 {NSErrorFailingURLStringKey= http://myAPI.com/ , _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey= http:// /myAPI.com , _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x145b21d0 “操作无法完成。(kCFErrorDomainCFNetwork 错误 -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:
    }
}

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".如果您忘记提及 HTTPMethod,也可能发生错误,我遇到了问题“NSURLErrorDomain Code=-1017”,不知何故我忘记添加行“request.HTTPMethod =“POST”。

After adding the line, my code worked perfectly.添加该行后,我的代码运行良好。

您的 JSON 参数有问题:

kCFURLErrorCannotParseResponse = -1017

当您使用主体集 ( setHTTPBody ) 执行GET请求时,会发生此错误

For any other people who got error code -1017 -- I fixed it by manually setting my http headers in my http request.对于任何其他收到错误代码-1017 ——我通过在我的 http 请求中手动设置我的 http 标-1017修复它。 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" .我认为服务器在解析 HTTP 标头时遇到问题,因为不是字符串"Authorization" : "qii2nl32j2l3jel"我的标头没有这样的引号: 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];

PLease check this link请检查此链接

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

in post method you also add在 post 方法中,您还添加

parameters:parameters, encoding: .JSON

(if you added encoding line to GET POST then error will come "cannot parse response") (如果您在 GET POST 中添加了编码行,则会出现“无法解析响应”错误)

2nd please check the header as well. 2nd 请检查标题以及。

["authentication_token" : "sdas3tgfghret6grfgher4y"]

then Solved this issue.然后解决了这个问题。

It isn't direct solution to this case but might help someone with -1017 error.这不是这种情况的直接解决方案,但可能会帮助出现-1017错误的人。

I had this issue after upgrading Alamofire from 4.8.0 to 5.2.2 .Alamofire4.8.0升级到5.2.2后,我遇到了这个问题。 Problem was with HTTPHeader .问题出在HTTPHeader

var authorizationHeader: HTTPHeader {
    guard let session = user?.session else {
        return HTTPHeader(name: "", value: "") 
    }
    return HTTPHeader(name: "Authorization", value: "Bearer \(session)")
}

Apparently sending HTTPHeader with empty key-value ie HTTPHeader(name: "", value: "") triggers this error.显然,发送带有空键值的HTTPHeader ,即HTTPHeader(name: "", value: "")会触发此错误。 I adjusted my code to instead return nil and it solved the issue.我调整了我的代码以返回nil ,它解决了这个问题。

For me when send one empty header like ["":""].对我来说,当发送一个像 ["":""] 这样的空标题时。 Triggers this error 1017.触发此错误 1017。

Solution, if you have declare:解决方案,如果您已声明:

let headers = ["":""]让标题 = ["":""]

the best one option is set to nil:最好的一个选项设置为 nil:

let headers = nil让标题= nil

Then you can:然后你可以:

sessionManager.request( "www.example.com", method: .post, parameters: parameters, encoding: encoding, headers: headers ).responseJSON { response in.... sessionManager.request( "www.example.com", method: .post, parameters: parameters, encoding: encoding, headers: headers ).responseJSON { response in....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Error Domain = NSURLErrorDomain代码= 403“操作无法完成。 (NSURLErrorDomain错误403。)” - Error Domain=NSURLErrorDomain Code=403 “The operation couldn’t be completed. (NSURLErrorDomain error 403.)” ObjectiveC + SDWebImageView:错误域= NSURLErrorDomain代码= 400“操作无法完成。 (NSURLErrorDomain错误400。)” - ObjectiveC+SDWebImageView : Error Domain=NSURLErrorDomain Code=400 “The operation couldn’t be completed. (NSURLErrorDomain error 400.)” 错误域=NSURLErrorDomain 代码=-1017 “无法解析响应” - Error Domain=NSURLErrorDomain Code=-1017 “cannot parse response” NSURLErrorDomain代码= -1012无法完成操作 - NSURLErrorDomain Code=-1012 The operation couldn’t be completed 操作无法完成。 (NSURLErrorDomain 错误 -1012。) - The operation couldn’t be completed. (NSURLErrorDomain error -1012.) AFNetworking:操作无法完成。(NSURLErrorDomain错误-1004) - AFNetworking : The Operation couldn't completed.(NSURLErrorDomain error -1004) Error Domain = NSCocoaErrorDomain代码= 512“该操作无法完成。该操作无法完成。是一个目录 - Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed.The operation couldn’t be completed. Is a directory 错误域=解析代码= 209“该操作无法完成 - Error Domain=Parse Code=209 "The operation couldn’t be completed ionic无法加载网页,并显示以下错误:操作无法完成。 (NSURLErrorDomain错误-999。) - ionic failed to load webpage with error: The operation couldn’t be completed. (NSURLErrorDomain error -999.) Facebook对话框失败并显示错误:无法完成操作。 (NSURLErrorDomain错误-999。) - Facebook dialog failed with error: The operation couldn’t be completed. (NSURLErrorDomain error -999.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM