iOS 无法执行 HTTP GET 请求 -Error Domain=NSURLErrorDomain Code=-1012

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22111801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:37:22  来源:igfitidea点击:

iOS Can't perform HTTP GET request -Error Domain=NSURLErrorDomain Code=-1012

ioshttpget

提问by Tvd

I am trying to perform a HTTP Get request, but I keep on getting Error Domain=NSURLErrorDomain Code=-1012 error. MY code is :

我正在尝试执行 HTTP Get 请求,但我不断收到 Error Domain=NSURLErrorDomain Code=-1012 错误。我的代码是:

@try {
    NSString *url = [[NSString alloc] initWithFormat:@"%@%@", @"http://api.mintchat.com/agent/chats/", agentSecret];

    NSLog(@"******Agents Active List URL Str - %@", url);
    NSURL *nsUrl = [NSURL URLWithString:url];  
    NSLog(@"******Agents Active List URL - %@", nsUrl);

   // Make the request, returning a JSON response, just as a plain old string.
   //NSData *jsonDataString = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error: nil] dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:nsUrl];
    [request setHTTPMethod:@"GET"];
    [request setValue:API_ID forHTTPHeaderField:@"X-API-ID"];
    [request setValue:API_SECRET forHTTPHeaderField:@"X--API--SECRET"];

    NSHTTPURLResponse *response = nil;
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response Code %d", [response statusCode]);
    if ([response statusCode] >= 200 && [response statusCode] < 300) {
        NSString *responseData = [ [NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
       NSLog(@"Response  ==> %@", responseData);        
    } else {
       if (error)
          NSLog(@"ERror : %@", error);
    }
}@catch(NSException *e) {
        NSLog(@"Exception - %@", e);
   }

I am to get response in JSon, but as things are not working so we are trying with some normal response. I confirmed the Headers to be added to the request. I need to add the both values to the headers. The url is also proper.

我将在 JSon 中得到响应,但由于事情不起作用,所以我们正在尝试一些正常的响应。我确认将标题添加到请求中。我需要将这两个值添加到标题中。网址也正确。

The results/logs I get is : Response Code 0

我得到的结果/日志是:响应代码 0

Error : Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn't be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x8abf470 { NSErrorFailingURLKey=.......NSErrorFailingURLStringKey=......NSUnderlyingError=0x8dafed0 "The operation couldn't be completed. (kCFErrorDomainCFNetwork error -1012.)" }

错误:错误域=NSURLErrorDomain 代码=-1012“操作无法完成。(NSURLErrorDomain 错误-1012。)” UserInfo=0x8abf470 { NSErrorFailingURLKey=.......NSErrorFailingURLStringKey=......NSUnderlyingError= 0x8dafed0 "操作无法完成。(kCFErrorDomainCFNetwork 错误 -1012。)" }

I also searched a lot on net reg this error, bit couldn't get any substantial help. I also confirmed regarding access to the web server api. With other url also I get the same results. From 3 urls, I could achieve results of 1st url using POST, but those other 2 urls - just could't get thru it.

我也在网络注册这个错误上搜索了很多,有点无法得到任何实质性的帮助。我还确认了对 Web 服务器 api 的访问。对于其他网址,我也得到了相同的结果。从 3 个 url,我可以使用 POST 获得第 1 个 url 的结果,但其他 2 个 url - 无法通过它。

Can anyone please help me with this problem. Where am going wrong ? Why can't access the api's. Any help is highly appreciated. Thanks

任何人都可以帮我解决这个问题。哪里出错了?为什么不能访问api的。任何帮助都受到高度赞赏。谢谢

采纳答案by msk

This error is NSURLErrorUserCancelledAuthentication

这个错误是NSURLErrorUserCancelledAuthentication

Returned when an asynchronous request for authentication is cancelled by the user. This is typically incurred by clicking a “Cancel” button in a username/password dialog, rather than the user making an attempt to authenticate.

当用户取消异步身份验证请求时返回。这通常是通过单击用户名/密码对话框中的“取消”按钮而不是用户尝试进行身份验证引起的。

It means you need authentication to access the resource which you are trying to access w/o signing your request properly (wrong agentSecret or API_ID or API_SECRET)

这意味着您需要身份验证才能访问您尝试访问的资源,而无需正确签署您的请求(错误的 agentSecret 或 API_ID 或 API_SECRET)

回答by manroe

You may get this error while hitting a HTTPS (not HTTP) URL with a bad/expired certificate with AFNetworking. (Chrome shows the "Your connection is not private" message when hitting the URL).

使用 AFNetworking 访问带有错误/过期证书的 HTTPS(而非 HTTP)URL 时,您可能会收到此错误。(Chrome 在点击 URL 时会显示“您的连接不是私密的”消息)。

回答by jatin

For my case i just set Yes for following flag of afnetworking http client. allowsInvalidSSLCertificate = YES, it starts working for https get request.

对于我的情况,我只是为以下 afnetworking http 客户端的标志设置了 Yes。allowInvalidSSLCertificate = YES,它开始为 https get 请求工作。

回答by inorganik

If it helps anyone, you will get this error whenever a server sends back a 401(unauthorized) status code.

如果它对任何人有帮助,只要服务器发回401(未经授权的)状态代码,您就会收到此错误。