问题描述
我正在发布一个小图像,所以我希望超时间隔很短.如果图像在几秒钟内没有发送,它可能永远不会发送.由于某种未知的原因,我的 NSURLConnection
永远不会失败,无论我设置的 timeoutInterval
有多短.
I'm POST'ing a small image, so i'd like the timeout interval to be short. If the image doesn't send in a few seconds, it's probably never going to send. For some unknown reason my NSURLConnection
is never failing, no matter how short I set the timeoutInterval
.
// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:@"http://www.tumblr.com/api/write"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:0.00000001];
/* Populate the request, this part works fine */
[NSURLConnection connectionWithRequest:request delegate:self];
我在 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
上设置了一个断点,但它从未被触发.我的图片继续发布得很好,尽管 timeoutInterval
很小,但它们仍然出现在 Tumblr 上.
I have a breakpoint set on - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
but it's never being triggered. My images continue to be posted just fine, they're showing up on Tumblr despite the tiny timeoutInterval
.
推荐答案
有一个关于 Apple 开发的主题讨论此问题的论坛.显然在 iPhone OS 上,setter 要求 timeoutInterval 至少为 240 秒(4 分钟).这仅在 postBody 不为空时发生(通常在使用 POST 请求时).这看起来很疯狂,但显然它可以确保请求离开系统,即使 WWAN (3G) 接口可能需要几秒钟才能唤醒.240 秒似乎相当陡峭,因此他们建议设置一个计时器并在您的计时器触发时取消异步连接.我知道这看起来很愚蠢,但这是我唯一设法使 POST 请求超时的方法... :-(
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... :-(
这篇关于NSMutableURLRequest 不遵守我的 timeoutInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!