我有以下问题。在使用 HTTP
方法 POST
的 NSMutableURLRequest
上,为连接设置的超时间隔将被忽略。如果互联网连接出现问题(错误的代理、错误的 dns),url 请求会在大约 2-4 分钟后失败,但不会出现 NSLocalizedDescription = "timed out";
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.
如果使用的http
方法是GET
,它工作正常。
通过 https
连接是异步
。
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval:10];
//Set the request method to post
[request setHTTPMethod:@"POST"];
if (body != nil) {
[request setHTTPBody:body];
}
// Add general parameters to the request
if (authorization){
[request addValue: authorization forHTTPHeaderField:@"Authorization"];
}
[request addValue: WS_HOST forHTTPHeaderField:@"Host"];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[self addToQueueRequest:request withDelegate:delegate];
'
最佳答案
根据 Apple 开发者论坛上的帖子,POST 的最小超时间隔为 240 秒。任何比该时间短的超时间隔都将被忽略。
如果您需要更短的超时间隔,请使用异步请求和计时器,并根据需要在 NSURLConnection 上调用取消。
线程链接:here
关于ios - POST 请求未考虑 NSMutableURLRequest 超时间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1466389/