SCNetworkReachability 是否尊重 HTTP 代理设置?
恐怕答案是否定的,但我希望有人能够提供明确的答案,因为当前的 iOS SDK 文档中没有记录该答案。
我们看到 NSURLConnection 能够通过 HTTP 代理连接到 https://mysite.com 但是,因为在这种情况下,本地 DNS 的设置方式将导致 mysite.com 的 DNS 查找失败。在本例中,SCNetworkReachability 似乎正在尝试对 mysite.com 执行 DNS 查找,但失败。同时,NSURLConnection 能够连接。
我们已将 Apple Reachability 示例代码合并到我们的应用程序中,并使用 mysite.com 调用 SCNetworkReachabilityCreateWithName。
I'm afraid the answer to this is No, but I'm hoping someone can provide a definitive answer as it is not documented in the current iOS SDK documentation.
We're seeing a case where NSURLConnection is able to connect to https://mysite.com via an HTTP proxy but, because of the way the local DNS is setup in this case, DNS lookups for mysite.com will fail. In this case, it appears that SCNetworkReachability is trying to perform a DNS lookup for mysite.com and failing. Meanwhile, NSURLConnection is able to connect.
We have incorporated the Apple Reachability sample code into our app and are calling SCNetworkReachabilityCreateWithName with mysite.com.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法提供明确的答案,但我可以提供更多的经验证据和一些理由,证明答案是否定的。我有一个使用
SCNetworkReachabilityGetFlags
来检查特定主机是否可访问的应用程序(例如:www.mysite.com)。根据该可达性检查,它然后使用[NSMutableData dataWithContentsOfURL:]
下载数据。该应用程序一直运行良好,但最近我在工作中进行了一些编码,其中网络访问是通过公司 HTTP 代理进行的。在 iOS 模拟器(使用我的 Mac 上配置的代理设置)中运行应用程序时,可达性检查失败。起初我以为 iOS 模拟器可能没有使用 Mac 的代理设置,但模拟器中的 Mobile Safari 工作正常。因此,我删除了应用程序中的可达性检查,并且对
[NSMutableData dataWithContentsOfURL:]
的调用工作正常。这似乎表明 SCNetworkReachability 不尊重代理设置。考虑一下,如果您将 SCNetworkReachability 视为在 TCP/IP 级别运行,而不是在 HTTP 级别运行,这可能是正确的行为,就像在 Mac 上
ping google.com
一样。公司防火墙后面的电脑也无法工作。 HTTP 代理(顾名思义)是针对 HTTP 协议的,而不是针对整个 TCP/IP 堆栈的。阅读了这个有关可达性的问题的答案后,我倾向于完全放弃我的可达性检查。尽管到目前为止这只是模拟器中的问题,但在其他情况下(例如需要身份验证的公共 WiFi 热点)也可能会出现问题。
I can't provide a definitive answer, but I can provide more empirical evidence, and some justification, that the answer is NO. I have an app that uses
SCNetworkReachabilityGetFlags
to check whether a particular host is reachable (e.g.: www.mysite.com). Depending on that reachability check, it then uses[NSMutableData dataWithContentsOfURL:]
to download the data.The app has always worked fine, but recently I've been doing some coding at work where network access is via the corporate HTTP proxy. When running the app in the iOS Simulator (which uses the proxy settings configured on my Mac) the reachability check fails. At first I thought that perhaps the iOS Simulator wasn't using the Mac's proxy settings, but Mobile Safari in the simulator worked fine. So I removed the reachability check in my app and the call to
[NSMutableData dataWithContentsOfURL:]
worked fine. This would appear to indicate that SCNetworkReachability does not respect the proxy settings.Having thought about it, this is probably the correct behaviour if you view SCNetworkReachability as running at the TCP/IP level, not at the HTTP level, in the same way that
ping google.com
on a Mac/PC behind a corporate firewall doesn't work either. The HTTP proxy (as the name implies) is for the HTTP protocol, not the whole TCP/IP stack.Having read the answers to this question on Reachability I'm inclined to bin my reachability check altogether. Even though it's only been a problem in the simulator until now, it could be problematic in other situations (e.g. public WiFi hotspot that requires authentication).