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

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

问题描述

我有以下问题.在使用 HTTP 方法 POSTNSMutableURLRequest 上,为连接设置的超时间隔将被忽略.如果互联网连接有问题(错误的代理,错误的 dns),url 请求会在大约 2-4 分钟后失败,但不会出现 NSLocalizedDescription = "timed out";

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天全站免登陆