NSMutableURLRequest “The request timed out” issue

2019-04-12 10:48发布

I am trying to download data in my application by using the following code

NSURL *url = [NSURL URLWithString:@"my download url string"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.f];

NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];

[connection start];

but the problem is some times i am getting the following error

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1d5be240 
{NSErrorFailingURLStringKey=http://dr282zn36sxxg.cloudfront.net/datastreams/f-
d%3Afc7f649e1e3ba58452f67e3fa1f66f69a15b96b3ea585c946e4fa854%2BEPUB%2BEPUB.1, 
NSErrorFailingURLKey=http://dr282zn36sxxg.cloudfront.net/datastreams/f-
d%3Afc7f649e1e3ba58452f67e3fa1f66f69a15b96b3ea585c946e4fa854%2BEPUB%2BEPUB.1, 
NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1e1975b0 "The request 
timed out."}

so , can anybody suggest me how to solve this . . .

4条回答
Explosion°爆炸
2楼-- · 2019-04-12 11:10

Try using GCDs abstraction called [NSURLConnection sendAsynchronousRequest:queue:completionHandler::

EXAMPLE:

NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest: myUrlRequest queue: queue completionHandler: ^ (NSURLResponse *response, NSData *data, NSError *error)
{

    if ([data length] > 0 && error == nil)
        //doSomething With The data

    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        //time out error

    else if (error != nil)
        //download error
}];

DOES IT MAKING ANY DIFFERENCE?

查看更多
再贱就再见
3楼-- · 2019-04-12 11:22

The error message clearly states that the request has timed out - Get to a place you get better network speed or increase the request timeout, I see it as 60 now sometimes which may not be sufficient for image downloads.

查看更多
ら.Afraid
4楼-- · 2019-04-12 11:23

Increase the timeout interval like 150 secs .

查看更多
劫难
5楼-- · 2019-04-12 11:27

Just resend the request, I guess :) or make request timeout interval longer (like 120 secs).

查看更多
登录 后发表回答