use*_*844 32 post objective-c nsurlconnection ios
我有以下问题.在NSMutableURLRequest使用该HTTP方法POST时,将忽略为连接设置的超时间隔.如果互联网连接有问题(错误的代理,坏的DNS),网址请求在大约2-4分钟后失败,但没有 NSLocalizedDescription = "timed out";
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.
Run Code Online (Sandbox Code Playgroud)
如果使用的http方法是GET它工作正常.连接async结束了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];
'
Run Code Online (Sandbox Code Playgroud)
fre*_*ler 27
iOS 6已修复此问题.
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
[request setHTTPMethod:method];
[request setHTTPBody:requestBody];
NSLog(@"%f", [request timeoutInterval]);
//20.0 in iOS 6
//240.0 in iOS 4.3, 5.0, 5.1
Run Code Online (Sandbox Code Playgroud)
use*_*844 10
修正了Clay Chambers的建议:使用自定义计时器在子类中添加了一个计时器 NSURLConnection
if (needsSeparateTimeout){
SEL sel = @selector(customCancel);
NSMethodSignature* sig = [self methodSignatureForSelector:sel];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:self];
[invocation setSelector:sel];
NSTimer *timer = [NSTimer timerWithTimeInterval:WS_REQUEST_TIMEOUT_INTERVAL invocation:invocation repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
Run Code Online (Sandbox Code Playgroud)
在自定义取消方法中,连接被取消
[super cancel];
Run Code Online (Sandbox Code Playgroud)