UIWebView不加载HTTPS页面:错误域= NSURLErrorDomain代码= -999“操作无法完成。 (NSURLErrorDomain错误-999。)“ [英] UIWebView does not load HTTPS Page: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"

查看:222
本文介绍了UIWebView不加载HTTPS页面:错误域= NSURLErrorDomain代码= -999“操作无法完成。 (NSURLErrorDomain错误-999。)“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这个问题仍然没有答案!

我使用 UIWebView 加载以下网址:

https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655

https://buchung.salonmeister.de/place/#offer-details-page?id=907599&venueId=301655

http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8634e147-e13d-40f5-8954-2ac40cfea2a7/romantik_hotel_bergström?customHeader=true

http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8af6d1fd-af6d-4765-8025-9eb8fa05ea42/hotel%20undeloher%20hof?customHeader=true

请注意,以上网址不是我的网址,因此我无法更改其内容。

Note that the above urls are not my urls so I cannot change their content.

尝试加载时,我会获得以下内容错误来自 func webView(webView:UIWebView,didFailLoadWithError错误:NSError)

When trying to load I get the following error from func webView(webView: UIWebView, didFailLoadWithError error: NSError):


错误域= NSURLErrorDomain代码= -999操作无法完成。 (NSURLErrorDomain错误-999。)UserInfo = 0x7ff7d0fd6f20 {NSErrorFailingURLStringKey = https:// buchung .salonmeister.de / ort / 301655 / menue /#offerId = 907601& venueId = 301655 ,NSErrorFailingURLKey = https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655 }

Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x7ff7d0fd6f20 {NSErrorFailingURLStringKey=https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655, NSErrorFailingURLKey=https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655}

我在iOS7和iOS8上测试了下面的代码。
Mobile Safari加载这个页面没有任何问题,但在我的UIWebView中我什么都没看到。

I tested the code below on iOS7 and iOS8. Mobile Safari loads this page without any problems but in my UIWebView I see nothing.

以下是我使用的代码:

class ViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDataDelegate {
  @IBOutlet weak var webView: UIWebView!
  var request: NSURLRequest!
  var urlString: String!
  private var isDone: Bool = false
  private var failedRequest: NSURLRequest!

  override func viewDidLoad() {
    super.viewDidLoad()

    urlString = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655"
    request = NSURLRequest(URL: NSURL(string: urlString)!)
    webView.delegate = self
  }

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.webView.loadRequest(self.request)
  }

  // MARK: UIWebViewDelegate

  func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    println("shouldStartLoadWithRequest()")

    if !isDone {
      isDone = false

      println("shouldStartLoadWithRequest() 111")
      failedRequest = request
      webView.stopLoading()
      var connection = NSURLConnection(request: request, delegate: self, startImmediately: true)
      connection!.start()
      //      NSURLConnection(request: request, delegate: self)
      return false
    }
    println("shouldStartLoadWithRequest() -----------------------")
    return true
  }

  func webViewDidStartLoad(webView: UIWebView) {
    println("webViewDidStartLoad()")
  }

  func webViewDidFinishLoad(aWebView: UIWebView) {
    println("webViewDidFinishLoad()")
  }

  func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
    println("webView(): didFailLoadWithError(): \(error)")
  }

  // MARK: NSURLConnectionDataDelegate

  func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
    println("connection willSendRequestForAuthenticationChallenge")

    if challenge.previousFailureCount == 0 {
      self.isDone = true
      println("x1")
      let credential: NSURLCredential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
      challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)

    }
    else {
      println("x2")
      challenge.sender.cancelAuthenticationChallenge(challenge)
    }
  }

  func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
    println("connection didReceiveResponse")
    self.isDone = true

    connection.cancel()
    self.webView.loadRequest(self.failedRequest)
  }

  func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool {
    println("connection canAuthenticateAgainstProtectionSpace")
    return protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
  }
}

我在这里用上面的代码创建了一个示例项目: https:// github.com/confile/UIWebView-https-url

I created a sample project with the above code here: https://github.com/confile/UIWebView-https-url

如何获取 UIWebView 正确加载网址?

How do I get my UIWebView load the url correctly?

推荐答案

我运行了你的代码。它在大约50%的时间内工作正常,并且在其他时间显示-999错误。

I ran your code. It worked fine approximately 50% of the time, and showed the -999 error the other times.

该错误代码是 NSURLErrorCancelled ,根据网址在Apple的Foundation Constants参考中加载系统错误代码。所以有些东西正在取消页面加载。

That error code is NSURLErrorCancelled, as per URL Loading System Error Codes in Apple's Foundation Constants Reference. So something is cancelling the page load.

我打印了收到的页面内容,使用:

I printed the page content that was received, using this:

println(aWebView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML"));

我看到页面中的Javascript包含:

I see that the Javascript in the page contains this:

if (mobilePath) {
   document.body.style.display = 'none';
   document.location = mobilePath;  
}

我的猜测是,一旦内容加载,这个Javascript就会开始运行。这会导致竞争条件,有时页面会立即重定向到另一个 document.location ,因此第一页加载会在完全加载之前被取消。

My guess is that this Javascript starts to run as soon as the content is loaded. This results in a race condition where sometimes the page is immediately redirected to a different document.location, and so the first page load is cancelled before it loads completely.

您应该更改服务器,使其不发送此Javascript。

You should change the server so that it does not send this Javascript.

或者,如果您只想加载网站像普通网站一样,您可以让UIWebView为您处理重定向。对于这个站点,我通过将User-agent设置为伪装成移动Safari来使其正常工作。我还删除了 shouldStartLoadWithRequest ,因为它只是干扰了事情。

Alternatively, if you just want to load the website like a normal website, then you can let UIWebView handle the redirects for you. In the case of this site, I got it working properly by setting the User-agent to pretend to be mobile Safari. I also removed shouldStartLoadWithRequest because it was only interfering with things.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
    var userAgent = ["UserAgent": "mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11a501 safari/9537.53"]
    NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])

    self.webView.loadRequest(request)
  }

如果我没有假装成为移动版Safari的代码,那么页面内容会加载,但它不会做正确的事情。它只是停在旋转器上。我认为该网站正在发送不同的Javascript,具体取决于它认为正在与之交谈的渲染引擎。如果你没有指定任何用户代理,那么你就可以接受他们的网页设计师在指定默认行为方面所做的一切。

If I don't have the code to pretend to be mobile Safari, then the page content loads but it doesn't do the right thing. It just stops at a spinner. I presume that the website is sending different Javascript depending on which rendering engine it thinks it is talking to. If you don't specify any User-agent then you are at the mercy of whatever their web designer has done in terms of specifying the default behavior.

这篇关于UIWebView不加载HTTPS页面:错误域= NSURLErrorDomain代码= -999“操作无法完成。 (NSURLErrorDomain错误-999。)“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆