我正在发布一个小图像,所以我希望超时间隔很短。如果图像在几秒钟内没有发送,它可能永远不会发送。不知什么原因我的NSURLConnection
永远不会失败,无论我设置的 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 上.
最佳答案
有a thread on Apple dev forums discussing this issue .显然在 iPhone OS 上,setter 要求 timeoutInterval 至少为 240 秒(4 分钟)。这只发生在 postBody 不为空时(通常在使用 POST 请求时)。这看起来很疯狂,但显然它的存在是为了确保请求离开系统,即使 WWAN (3G) 接口(interface)可能需要几秒钟才能唤醒。 240 秒似乎相当陡峭,因此他们建议设置一个计时器并在您的计时器触发时取消异步连接。我知道这看起来很愚蠢,但这是我唯一设法让 POST 请求超时... :-(
关于nsurlconnection - NSMutableURLRequest 不遵守我的 timeoutInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2736967/