How to wait long time of http get requests?

I use this code to get requests


- (void)viewDidLoad {
    NSString *str = [self getDataFrom:@"http://61.218.228.98:8008/Verify.aspx?EleSignVerift=553495ED"];
    NSLog(@"%@",str);
    [super viewDidLoad];
}

- (NSString *) getDataFrom:(NSString *)url{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"GET"];
    [request setURL:[NSURL URLWithString:url]];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *responseCode = nil;

    NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

    if([responseCode statusCode] != 200){
        NSLog(@"Error getting %@, HTTP status code %li", url, (long)[responseCode statusCode]);
        return nil;
    }

    return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
}


However, this request will be made to spend a very long time

This makes the result is "HTTP status code 0"

How do I fix it?

Accepted Answer

I found the way!

Use NSMutableURLRequest


-(void)aget:(NSString *)iurl{
    NSLog(@"ster down");
    //NSURL*url = [NSURL URLWithString:iurl];
    NSMutableURLRequest *res = [[NSMutableURLRequest alloc]init]; //Emphasis
    NSOperationQueue*que=[NSOperationQueue new];
    [res setTimeoutInterval:300]; //Emphasis
    [res setHTTPMethod:@"GET"]; //Emphasis
    [res setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",iurl]]]; //Emphasis
    [NSURLConnection sendAsynchronousRequest:res queue:que completionHandler:^(NSURLResponse*rep,NSData*data,NSError*err){
        if ([data length]> 0 && err == nil) {
            NSString* rel=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",rel);
        }else{
            NSLog(@"isnull");
        }

    }
     ];
}

This may be an issue with the new App Transport Security feature of iOS 9.0 and OS X 10.11, which might be forcing your connection to go over https and result in errors if the server you are contacting doesn't offer compatible support.


https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html


https://forums.developer.apple.com/thread/3544

I think this is the

But I found a way, just use NSMutableURLRequest

Instead NSURLRequest

The problem is solved

Thank you for helping me 😀

That will work, with several caveats:


  • If the device goes to sleep before the request finishes, you probably won't get your data.
  • You're keeping a TCP connection open continuously for five minutes, which wastes battery power by keeping the network interfaces powered up.
  • You're tying up one of the client slots on your web server for five minutes, which could be problematic if you're using Apache and you have a lot of users doing this at the same time.


If this is for internal testing purposes, that's probably fine, but if you plan to ship an app that does this, assuming you have control over both the server and the client, you'd probably be better off using a slightly modified approach:


  1. Make an intial request to the web server that starts the long-running task.
  2. Make additional requests every few seconds to ask whether the task has finished (and if so, to obtain the result data).


This allows the networking stack and the server to better manage the connection. If the server has too many clients waiting, it can refuse to keep the connection alive. If the client needs to turn off its networking hardware, it can do so. If the iOS device goes to sleep, it will query the server when it wakes up again, and it will obtain the result of that long-running task at that time.

How to wait long time of http get requests?
 
 
Q