设置请求超时的时间

  1. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  2. - (void)viewDidLoad  
  3. {  
  4.     [super viewDidLoad];  
  5.       
  6.   
  7.     NSLog( @"this is First in viewDidLoad" );  
  8.   
  9.     int defaultTimeout = 3.0f;  
  10.     NSString *str = @"http://www.baidu.com";  
  11.     NSURL *url = [NSURL URLWithString: str];  
  12.     NSString *outputBody = @"yes";  
  13.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url  
  14.                                                            cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData  
  15.                                                        timeoutInterval:defaultTimeout];  
  16.     NSData *bodyData = [outputBody dataUsingEncoding:NSUTF8StringEncoding];  
  17.       
  18.     NSArray *cookies = [NSArray array];  
  19.     if(cookies != nil) {  
  20.         [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];  
  21.     }  
  22.     [request setValue:@"wsdl2objc" forHTTPHeaderField:@"User-Agent"];  
  23.     [request setValue:@"soapAction" forHTTPHeaderField:@"SOAPAction"];  
  24.     [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
  25.     [request setValue:[NSString stringWithFormat:@"%u", [bodyData length]] forHTTPHeaderField:@"Content-Length"];  
  26.     [request setValue: str forHTTPHeaderField:@"Host"];  
  27.     [request setHTTPMethod: @"POST"];  
  28.       
  29.     [request setHTTPBody: bodyData];  
  30.       
  31.     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:nil];  
  32.       
  33.     //自定义时间超时  
  34.     [NSTimer scheduledTimerWithTimeInterval:defaultTimeout   
  35.                                      target: self   
  36.                                    selector: @selector(handleTimer)   
  37.                                    userInfo:nil   
  38.                                     repeats:NO];  
  39.       
  40.       
  41.     [connection release];  
  42. }  
  43.   
  44.   
  45. //时间超时定义  
  46. -(void) handleTimer  
  47. {  
  48.     //请求超时后,要执行的操作,比如:退出,弹出提示等。。。  
  49.     NSLog( @"超时了,推出程序" );  
  50. }  


There's a thread on Apple dev forums discussing this issue. Apparently on iPhone OS, the setter mandates timeoutInterval a minimum of 240 seconds (4 minutes). This only occurs when the postBody is not empty (typically when using a POST request). This seems crazy, but apparently it's there to make sure requests leave the system even though it might take many seconds for the WWAN (3G) interface to wake up. 240 seconds seems rather steep, so they suggest to set a timer and cancel the asynchronous connection when your timer fires. I know this seems stupid, but that's the only I managed to get timeout for POST requests... :-(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值