POST请求未考虑NSMutableURLRequest超时间隔 [英] NSMutableURLRequest timeout interval not taken into consideration for POST requests

查看:325
本文介绍了POST请求未考虑NSMutableURLRequest超时间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。在 NSMutableURLRequest 上使用 HTTP 方法 POST 超时间隔为连接设置被忽略。如果互联网连接有问题(错误的代理,坏dns),网址请求在大约2-4分钟后失败,但没有 NSLocalizedDescription =超时;

I have the following problem. On a NSMutableURLRequest using the HTTP method POST the timeout interval set for the connection is ignored. If the internet connection has a problem (wrong proxy, bad dns) the url request fails after about 2-4 minutes but not with NSLocalizedDescription = "timed out";

NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1001 UserInfo=0x139580 "The request timed out.

如果使用的 http 方法是 GET 它工作正常。
连接 async 超过 https

If the http method used is GET it works fine. The connection is async over 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秒。忽略任何短于此的超时间隔。

According to a post on the Apple developer forum, the minimum timeout interval for POST is 240 seconds. Any timeout interval shorter than that is ignored.

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

If you require a shorter timeout interval, use an async request along with a timer and call cancel on the NSURLConnection as needed.

指向主题的链接:此处

这篇关于POST请求未考虑NSMutableURLRequest超时间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆