UIWebView:为什么使用不存在的主机名时请求不会超时? [英] UIWebView: Why does the request not time out when using a non-existant hostname?

查看:58
本文介绍了UIWebView:为什么使用不存在的主机名时请求不会超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是这样:

  • 我有一个UIWebView,它向由用户提供主机名的URL发出POST请求.
  • 当主机名不存在时,我需要超时,以便提醒用户说他们应该检查其设置.
  • I have a UIWebView which makes a POST request to a URL where the hostname is provided by the user.
  • When the hostname does not exist, I need to timeout so that I can alert the user to say that they should check the their settings.

发生了什么事

我这样请求:

NSString *theURL = [NSString stringWithFormat:@"https://%@%@", _hostname, LOGIN_PART_URL];
NSString *body = [NSString stringWithFormat:@"Username=%@&Password=%@", _username, _password];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:theURL] 
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:_timeoutInSecs];



[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];

[web loadRequest:request];

当主机名在网络上不存在时,我没有收到任何超时响应.

When the hostname does not exist on the network, I do not get any timeout response.

我已经实现了UIWebViewDelegate的所有委托方法,当我使用不存在的主机名时,将依次调用以下命令:

I have implemented all delegate methods of UIWebViewDelegate, and I get the following called in order when I use a non-existent hostname:

  1. webView: shouldStartLoadWithRequest: navigationType:
    • (对于测试始终返回true)

就是这样-之后什么也没有.

And that's it - nothing after that.

我很惊讶webViewDidStartLoad被调用,因为这表明它找到了URL,并认为它可以从中获取数据.但是不能,因为URL不存在!

I'm surprised that webViewDidStartLoad gets called, as this would suggest that it found the URL and thinks it can get data back from it. But it can't as the URL does not exist!

我在这里做错什么了吗?

Am I doing something wrong here?

我要实现一个变通方法,该变通方法使用NSTimer来查看是否在超时时间内已收到响应.但是,我在取消UIWebView的请求时遇到问题-但这是我的下一个SO问题的主题!

I have a workaround which I am going to implement, which uses an NSTimer to see if a response has been received within the timeout period. However, I am having issues with cancelling a request for a UIWebView - but that's a topic for my next SO question!

所以我的问题:

  1. 在无效的URL上调用webViewDidStartLoad是否正确?
  2. 是否应该以我设置请求的方式看到请求超时错误返回?
  1. Is it correct for webViewDidStartLoad to be called on an invalid URL?
  2. Should I be seeing a request timeout error returned, with the way I have setup my request?

提前谢谢!

拉伸:)

推荐答案

使用委托方法

webView:didFailLoadWithError:

从中获取错误.如果错误代码是

Get the error from it. If the error code is

-1000 - Bad Url
-1001 - Request timed out
-1003 - Cannot find host
-1004 - Cannot connect to host
-1005 - Network connection lost
-1006 - NSLookupFailed

通过选中此复选框,可以提醒用户该错误.

By checking this you can alert the user about the error.

这篇关于UIWebView:为什么使用不存在的主机名时请求不会超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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