nsurlconnection - NSMutableURLRequest 不遵守我的 timeoutInterval

标签 nsurlconnection nsurlrequest nsmutableurlrequest

我正在发布一个小图像,所以我希望超时间隔很短。如果图像在几秒钟内没有发送,它可能永远不会发送。不知什么原因我的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/

相关文章:

ios - 搜索时在字符类型上创建 url 请求的好方法

ios - 解析来自 URL 的 JSON 响应给出 <null>

ios - 需要更改 NSURLConnection 方法以通过身份验证质询

HTML 形式的 iOS NSURLRequest

iphone - NSMutableURLRequest 在正文中添加额外信息,导致 Content-Length 不正确

ios - 如何从 AppDelegate 调用不同类的实例方法?

objective-c - UIWebView 和 NSURLRequest 上没有 Internet 连接处理

ios - iOS11中如何修改NSURLRequest http方法?

xcode - 在 Swift 中从 NSString 创建 NSData

ios - 从 Swift 5 iOS 应用程序进行 Mailchimp API 调用时“API key 丢失”