GCD 未获取 Web 内容以获取在 Internet 上部署的字符串

Posted

技术标签:

【中文标题】GCD 未获取 Web 内容以获取在 Internet 上部署的字符串【英文标题】:GCD not getting the web content to get the string deployed on the Internet 【发布时间】:2014-06-10 08:19:55 【问题描述】:

我正在开发获取当前时间在网页上显示为 10-06-2014 16:19:11 的后台线程模块。当涉及到执行时,没有数据被获取。您能告诉我如何创建 inputStream 并阅读网页内容吗? 假设 https 链接有效 给定消息是:此服务器的证书无效。您仍然要连接到服务器吗?,

以下是我的留言

2014-06-10 16:46:16.292 marker[2724:7707] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
2014-06-10 16:46:16.296 marker[2724:60b] Error = Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “dc02.cei-hk.net” which could put your confidential information at risk.

" UserInfo=0x146be370 NSErrorFailingURLStringKey=https://dc02.cei-hk.net:8081/GetTime.aspx, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, 
NSErrorFailingURLKey=https://dc02.csd-hk.net:8081/GetTime.aspx, NSLocalizedDescript`

ion=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “dc02.cei-hk.net” which could put your confidential information at risk., NSUnderlyingError=0x14582870 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “dc02.csd-hk.net” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x14675820>`

下面是我的代码

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^

    dispatch_async(dispatch_get_main_queue(), ^
        NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://dc02.csd-hk.net:8081/GetTime.aspx",nil]];
        NSError *error = nil;
        NSURLRequest *request = [NSURLRequest  requestWithURL:jsonURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0 ];

        NSData *responsedata = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
        NSString* jsonData = [[NSString alloc] initWithData:responsedata encoding:NSUTF8StringEncoding];

   //     NSString *json = [NSString stringWithContentsOfURL:url   encoding:NSUTF8StringEncoding error:&error];
        if( jsonData.length > 0 )
        
            NSLog(@"Text=%@", jsonData);
            [ToastView showToastInParentView:self.view withText:jsonData withDuaration:5.0];
        
        else 
        
            NSLog(@"Error = %@", error);
        
    );

);

【问题讨论】:

【参考方案1】:

您的网址无法解析,因此您最好检查两次。

查看this 以获取替代服务。

【讨论】:

现在发生的情况是您使用的服务器没有公认的 SSL 证书(OS X 上的 CURL 也出于同样的原因拒绝它)。要么你找到一个在 http 上运行的服务(所以你不需要证书),如果它适合你,或者你设置一些东西来忽略这里建议的错误:***.com/questions/6792213/…

以上是关于GCD 未获取 Web 内容以获取在 Internet 上部署的字符串的主要内容,如果未能解决你的问题,请参考以下文章

使用 extract($variables) 获取内容,但变量未定义

如何根据wsdl获取SOAP内容

推送以唤醒后台 iOS 8 应用程序未获取任何数据

使用 Flutter web (chrome) 从系统目录获取文件路径以读取文件内容 例如:CSV 或文本文件

共享资源访问 + UI 更新的 GCD 模式?

在 Swift 3 中获取 GCD 标签