菜鸟教程小白 发表于 2022-12-12 20:44:41

ios - 无法使用 HTTPS 连接到服务器


                                            <p><p>首先,我知道有 <strong><em>吨</em></strong> 类似的问题,但我所见过的似乎都没有解决我的设置问题(我发现的任何解决方案都没有工作)。所以请耐心等待...</p>

<ul>
<li>我的服务器主机名是 IP 地址,不是域名
(即,URL 看起来像:<code>https://XXX.YYY.ZZZ.WWW:9443/etc...</code>)。</li>
<li>我的服务器有一个真实的证书(即<strong>不是</strong>自签名的)。</li>
<li>我的应用的 plist 条目 <code>NSAppTransportSecurity</code> 字典为空(没有任何异常(exception) - 出厂设置 ATS)。</li>
<li>这是生产代码,我可以<strong>不</strong>禁用 ATS(我认为我也不能,因为异常(exception)仅适用于明确的域名,<em>不是</em> IP 地址)。 </li>
</ul>

<p><em>(在 iOS 9 上测试,部署目标是 iOS 8.x)</em></p>

<p><strong>我在尝试连接时收到此错误:</strong></p>

<blockquote>
<p>CFNetwork SSLHandshake failed (-9806) NSURLSession/NSURLConnection
HTTP load failed (kCFStreamErrorDomainSSL, -9806)
Error: An SSL error has occurred and a secure connection to the server cannot be made.</p>
</blockquote>

<p><em>(设备<strong>和</strong>模拟器)</em></p>

<p>我尝试用命令行工具<code>nscurl</code>描述<a href="https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW41" rel="noreferrer noopener nofollow">here</a> .我明白了:</p>

<ul>
<li><strong>默认 ATS 安全连接:</strong> CFNetwork SSLHandshake 失败 (<strong>-9806</strong>)</li>
<li><strong>允许任意加载:</strong> NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,<strong>-9813</strong>)<em>(“此服务器的证书无效。您可能正在连接到伪装成“XXX.YYY.ZZZ.WWW”的服务器,这可能会使您的 secret 信息面临风险。”)</em></li>
<li><p><strong>为 XXX.YYY.ZZZ.WWW 配置 TLS 异常(exception):</strong>(TLS 1.2、1.1 和 1.0)CFNetwork SSLHandshake 失败 (<strong>-9806</strong>)</p ></li>
<li><p><strong>禁用完美前向保密:</strong> CFNetwork SSLHandshake 失败 (<strong>-9801</strong>)</p></li>
<li><p><strong>禁用完美前向保密并允许不安全的 HTTP:</strong> CFNetwork SSLHandshake 失败 (<strong>-9801</strong>)</p></li>
<li><p><strong>禁用 PFS 的 TLSv1.2:</strong> CFNetwork SSLHandshake 失败 (<strong>-9801</strong>)</p></li>
<li><p><strong>禁用 PFS 的 TLSv1.1:</strong> CFNetwork SSLHandshake 失败 (<strong>-9801</strong>)</p></li>
<li><p><strong>禁用 PFS 的 TLSv1.0:</strong> NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,<strong>-9802</strong>)</p></li>
<li><p><strong>禁用 PFS 且允许不安全 HTTP 的 TLSv1.2:</strong> CFNetwork SSLHandshake 失败 (<strong>-9801</strong>)</p></li>
</ul>

<p>...你得到了演习。</p>

<p>我正在检查服务器支持哪个 <strong>TLS 版本</strong>(据我研究,这是最大的嫌疑人),但也许还有其他问题我需要修复/检查客户端?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您需要检查服务器上的证书。您应该能够使用 openssl 客户端来调查您的证书并获取服务器的 ssl 配置:</p>

<pre><code>openssl s_client-connect XXX.YYY.ZZZ.WWW:9443
</code></pre>

<p>您应该获得有关证书的一些详细信息</p>

<p>SSLsession :
    协议(protocol):TLSv1
    密码:AES256-SHA
   sessionID://
   sessionID-ctx:
    主 key ://
    键-Arg:无
    开始时间:1449693038
    超时:300(秒)
    验证返回码:0(ok)</p>

<p>或者,您可以使用 <a href="https://cryptoreport.websecurity.symantec.com/checker/views/certCheck.jsp" rel="noreferrer noopener nofollow">symantec</a> 之类的网站查询证书,看看是否满足 TLS1.2、足够强的 key 和前向保密的要求。 </p>

<p>另外,您可以尝试打开 CFNetwork 诊断日志记录。编辑 Xcode 方案并添加 CFNETWORK_DIAGNOSTICS 环境变量。将日志记录级别设置为最详细的 3:</p>

<p>Xcode 控制台显示日志文件的位置:</p>

<pre><code>CFNetwork diagnostics log file created at: /private/var/mobile/Containers/
Data/Application/A3421F00-451A-CD70-1B82-B163D1A3BB0F/Library/Logs/
CrashReporter/CFNetwork_com.sample.app_118.nwlrb.log
</code></pre>

<p>您可以查看这些日志,看看是否有更多信息说明网络调用失败的原因。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法使用 HTTPS 连接到服务器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37852829/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37852829/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法使用 HTTPS 连接到服务器