1

What is the best way to implement a connection timeout (let's say, 20 seconds) within an HTTP post connection?

My current code is as follows:

-(NSData*) postData: (NSString*) strData
{    
    //postString is the STRING TO BE POSTED
    NSString *postString;

    //this is the string to send
    postString = @"data=";
    postString = [postString stringByAppendingString:strData]; 

    NSURL *url = [NSURL URLWithString:@"MYSERVERHERE"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];

    //setting prarameters of the POST connection
    [request setHTTPMethod:@"POST"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"en-US" forHTTPHeaderField:@"Content-Language"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setTimeoutInterval:10.0];

    NSLog(@"%@",postString);

    NSURLResponse *response;
    NSError *error;

    NSLog(@"Starting the send!");
    //this sends the information away. everybody wave!
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"Just finished receiving!");
    if (&error) //OR TIMEOUT
    {
        NSLog(@"ERROR!");
        NSString *errorString = [NSString stringWithFormat:@"ERROR"];
        urlData = [errorString dataUsingEncoding:NSUTF8StringEncoding];
    }
    return urlData;
}

Obviously the timeout interval is set to 10.0, but nothing seems to happen when those ten seconds hit.

jscs
  • 63,694
  • 13
  • 151
  • 195
Baub
  • 5,004
  • 14
  • 56
  • 99
  • have you implemented the `NSURLConnection` delegate methods? – Fran Sevillano Sep 26 '11 at 16:56
  • Looking through the delegate methods, I do not see one that mentions timeout. connection:didReceiveResponse:, connection:didReceiveData:, connection:didFailWithError: and connectionDidFinishLoading:. – Baub Sep 26 '11 at 17:07
  • Well, `connection:didFailWithError` will be called when the request hits the timeout – Fran Sevillano Sep 26 '11 at 17:20
  • 1
    Take a look at this question though, it's about post requests http://stackoverflow.com/questions/2736967/nsmutableurlrequest-not-obeying-my-timeoutinterval – Fran Sevillano Sep 26 '11 at 17:22
  • Looking through the apple docs though, they show a timeout of 60 which, of course, does not hit their minimum interval of 240. What are your thoughts?http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html – Baub Sep 26 '11 at 17:31
  • Test it and see if its 60 or 240 secs. If you need a shorter timeout, do as the question i linked to says and set a timer. When it expires, close the connection. – Fran Sevillano Sep 26 '11 at 17:43

1 Answers1

1

See:

NSMutableURLRequest timeout interval not taken into consideration for POST requests

Apparently timeouts under 240 seconds are ignored. The highest voted answer in that question links to a solution. However, I would simply recommend using the ASIHTTPRequest library instead.

http://allseeing-i.com/ASIHTTPRequest/

Community
  • 1
  • 1
Jacob Jennings
  • 2,796
  • 1
  • 24
  • 26
  • Yet looking through Apples documents, in their sample code they use a timeout of 60. I'm quite confused. – Baub Sep 26 '11 at 17:40