gpt4 book ai didi

ios - 错误域=NSURLErrorDomain Code=-1017 "The operation couldn’ t be

转载 作者:可可西里 更新时间:2023-11-01 04:09:41 25 4
gpt4 key购买 nike

我刚开始 ios 开发,我正在尝试与我的 api 交换数据。当我执行 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.)"}

有人能解释一下出了什么问题以及我该如何解决吗?

我的要求:

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

最佳答案

如果您忘记提及 HTTPMethod,也会发生错误,我遇到了问题“NSURLErrorDomain Code=-1017”,不知何故我忘记添加行“request.HTTPMethod =”POST”。

添加该行后,我的代码运行完美。

关于ios - 错误域=NSURLErrorDomain Code=-1017 "The operation couldn’ t be,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com