HTTPポスト接続内で接続タイムアウト(20秒と言う)を実装する最良の方法は何ですか?HTTP POSTでタイムアウトを実装する
次のように私の現在のコードは次のとおりです。
-(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;
}
は明らかにタイムアウト間隔は10.0に設定されているが、何もそれらの10秒がヒット時に発生するようです。
あなたは 'NSURLConnection'デリゲートメソッドを実装しましたか? –
いいえ、私はしませんでした。 – Baub
デリゲートメソッドを見ても、タイムアウトについてはわかりません。接続:didReceiveResponse :,接続:didReceiveData :,接続:didFailWithError:およびconnectionDidFinishLoading :. – Baub