0

I have asynchronous request to send image to server with POST. I set timeoutInterval to 10seconds. If it sent image to 10 seconds, everything works good, but when user have worse internet connection, it should stop request after 10 seconds. But this request seems that it never stopped. Can you help?

- (void)postDataWithImage:(NSData *)imageData {

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@.%@",API_URL,self.method]]];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:10.0];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"unique-consistent-string";

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    for (NSString *param in self.parametersDictionary) {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@\r\n", [self.parametersDictionary objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    // add image data
    if (imageData) {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=place.jpg\r\n", @"i"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imageData];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%d", (int)[body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^(void){

        NSLog(@"Response %@",response);

        if ([data length] > 0) {
            NSLog(@"Success");
        } else {
            NSLog(@"Error"); // This never show me
        });

    }];

}

1 Answer 1

1

According to another question here on Stackoverflow, the default timeout for POST is 240 seconds and any shorter intervals are ignored.

Link to answer here

1
  • Also note that the behavior you're describing is subject to change at any time, in any iOS update, for any reason. :-) Either way, the normal assumption is that POST requests aren't idempotent, and thus can't be replayed safely. If your situation allows them to be replayed, or if you really just want to annoy users with slow networks, then by all means, set a timeout, but you will probably have to do it by scheduling an NSTimer and canceling the request.
    – dgatwood
    Oct 28, 2015 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.