Ios 错误域=NSURLErrorDomain代码=-1017“;操作无法’;不可能

Ios 错误域=NSURLErrorDomain代码=-1017“;操作无法’;不可能,ios,objective-c,Ios,Objective C,我刚刚开始ios开发,我正在尝试用我的api交换数据。当我执行POST请求时,一切正常,但当我尝试执行GET请求时,我会出现以下错误: 错误域=NSURLErrorDomain Code=-1017“无法执行该操作 已完成。(NSURlerDomain错误-1017。)“UserInfo=0x145a2c00 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=-1,NSErrorFailingURLKey=,, _kCFStreamErro

我刚刚开始ios开发,我正在尝试用我的api交换数据。当我执行POST请求时,一切正常,但当我尝试执行GET请求时,我会出现以下错误:

错误域=NSURLErrorDomain Code=-1017“无法执行该操作 已完成。(NSURlerDomain错误-1017。)“UserInfo=0x145a2c00 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=-1,NSErrorFailingURLKey=,, _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参数有问题:


kcfurerrorcannotparseresponse=-1017

对于任何其他获得错误代码的人
-1017
——我通过在http请求中手动设置http头来修复它。我认为服务器解析HTTP头时遇到问题,因为不是字符串“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];

如果忘记提到HTTPMethod,也可能会发生错误,我遇到了问题“NSURLErrorDomain Code=-1017”,不知怎的,我忘记了添加行“request.HTTPMethod=“POST”

添加行后,我的代码工作正常。

请检查此链接

Alamofire.request(method, "(URLString)" , parameters: parameters, headers: headers).responseJSON { response in }
在post方法中,您还可以添加

parameters:parameters, encoding: .JSON
(若您添加了编码行以获取POST,则错误将出现“无法解析响应”)

第二,请检查标题

["authentication_token" : "sdas3tgfghret6grfgher4y"]

然后解决了这个问题。

当您使用body set(
setHTTPBody
)执行
GET
请求时,会发生此错误。

这不是解决此问题的直接方法,但可能会帮助某人解决
-1017
错误

4.8.0
升级到
5.2.2
后,我遇到了这个问题。问题是
HTTPHeader

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

显然,使用空键值i-e发送
HTTPHeader
(名称:,值:)会触发此错误。我将代码调整为返回
nil
,从而解决了此问题。

对于我,当发送一个类似[“”:“”]的空标头时。触发此错误1017

解决方案,如果您已声明:

let headers=[“”:“”]

最佳选项设置为零:

设标题=nil

然后你可以:

sessionManager.request( “www.example.com”, 方法:.post, 参数:参数, 编码:编码, 标题:标题
).responseJSON{response in…

此问题的格式需要清理。