iPhone SDK: URL request not timing out

眉间皱痕 提交于 2020-01-10 20:01:35

问题


I am having a problem with a network request that should timeout, but the method is not called. The request is as follows:

#define kCONNECT_TIMEOUT 20.0

request = [NSMutableURLRequest requestWithURL: aUrl];
[request setHTTPMethod: @"POST"];
postData = [jsonData dataUsingEncoding:NSASCIIStringEncoding];
[request setHTTPBody:postData];
[request setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setCachePolicy:NSURLCacheStorageAllowed];
[request setTimeoutInterval:kCONNECT_TIMEOUT];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
assert(self.connection != nil);

This should get a callback to

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)_error 

But after 4 minutes no error message is displayed. Anyone know why this might be?


回答1:


A representative from Apple has divulged that SDK 3.0 and later enforce a minimum timeout of (you guessed it) four minutes:

https://devforums.apple.com/thread/25282

If you try to set a timeout value of less than 240 seconds, it gets clamped up to 240. If you need a shorter timeout, I cast my vote for St3fan's solution.




回答2:


If you want better timeout management on http requests using NSURLConnection then it is much better to run the request asynchronous together with an NSTimer that can cancel the NSURLConnection when it fires because the timeout period expired.

This also means you don't have to deal with threads, which is generally a good idea. Async event (runloop) based operations are the way to go in 99.9% of the cases on the iPhone.



来源:https://stackoverflow.com/questions/2471475/iphone-sdk-url-request-not-timing-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!