我正在使用一個NSMutableURLRequest
和NSURLConnection
將數據發送到我的服務器iOS應用程序。我需要在失敗時顯示錯誤消息。我的發送請求的代碼如下所示約15秒後NSMutableURLRequest超時。超時設置在240秒
const int TIMEOUT = 120;
-(void)send:(id<NSURLConnectionDelegate>)delegate
{
bodyContents = [bodyContents stringByAppendingFormat:@"--%@--",boundry];
NSData *data = [bodyContents dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%i",[data length]+4] forHTTPHeaderField:@"Content-Length"];
NSLog(@"Timeout before setting custom is %f",request.timeoutInterval);
[request setTimeoutInterval:TIMEOUT];
NSLog(@"Timeout after setting custom is %f",request.timeoutInterval);
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:delegate];
if(connection)
NSLog(@"Connection good");
else
NSLog(@"Connection not so good");
}
//in the delegate class
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSLog(@"connectionDidFinishLoading");
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error
{
NSLog(@"Failed with error %@",error);
btnCancel.title = @"Retry";
self.navigationItem.hidesBackButton = NO;
[self showAlertDialog:[NSString stringWithFormat:@"%@",[error localizedDescription]] :@"Error"];
}
我希望2分鐘無反應後要調用的didFailWithError消息。相反,它會在大約15秒沒有迴應的情況下被調用,這沒有任何意義。我查看了文檔以檢查是否正確設置了超時時間,但我能找到的唯一情況是帶有主體的帖子的最小超時時間爲240秒,這是我勉強接受的時間。
從運行的應用程序的輸出如下所示:
2012-06-22 16:49:12.393媒體上傳[672:707]設置定製之前超時是240.000000
2012-06 -22 16:49:12.394媒體上傳[672:707]設置的自定義後超時是240.000000
2012-06-22 16:49:12.395媒體上傳[672:707]連接好
2012-06 -22 16:49:29.174媒體上傳[672:70 7]失敗,錯誤錯誤域= NSURLErrorDomain代碼= -1001「請求超時。」 etc
我對它爲何如此快速地超時有點困惑。當連接創建時服務器沒有啓動,所以我甚至不知道爲什麼它說連接好,而不是連接不太好,但這是一個不同的問題(我希望)任何建議?
所以只是要確定我的理解,基本上是15ish秒超時是因爲它無法找到服務器最初時240秒超時是:「如果我已經聯繫了服務器和發送數據包我的,但它不響應240秒,然後超時。「是對的嗎? – nick
這是我的理解,是的。 240秒是'連接'超時,一旦你有'連接'就應用。 –