首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对于POST请求,没有考虑NSMutableURLRequest超时间隔

对于POST请求,没有考虑NSMutableURLRequest超时间隔
EN

Stack Overflow用户
提问于 2009-09-23 14:33:50
回答 5查看 41K关注 0票数 32

我有以下问题。在使用NSMutableURLRequest方法POSTHTTP上,将忽略为连接设置的超时间隔。如果internet连接有问题(错误的代理,错误的dns),url请求在大约2-4分钟后就会失败,但在NSLocalizedDescription = "timed out";中不会失败。

代码语言:javascript
运行
复制
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.

如果使用的http方法是GET,那么它工作得很好。连接是async over https

代码语言:javascript
运行
复制
    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];

'
EN

回答 5

Stack Overflow用户

发布于 2009-11-19 18:36:17

根据Apple developer论坛上的一篇文章,帖子的最低超时间隔为240秒。任何短于此的超时间隔都会被忽略。

如果需要更短的超时间隔,请使用异步请求和计时器,并根据需要在NSURLConnection上调用cancel。

链接到线程:这里

票数 28
EN

Stack Overflow用户

发布于 2012-10-15 17:45:48

iOS 6已经解决了这个问题。

代码语言:javascript
运行
复制
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
票数 28
EN

Stack Overflow用户

发布于 2010-05-14 15:13:58

修正了克莱钱伯斯的建议:使用定制计时器在NSURLConnection子类中添加一个计时器

代码语言:javascript
运行
复制
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];

}

在自定义取消方法中,连接被取消。

代码语言:javascript
运行
复制
[super cancel];     
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1466389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档