将NSMutableURLRequest的超时设置为自定义时间

时间:2011-06-14 05:03:11

标签: iphone objective-c ios cocoa-touch nsmutableurlrequest

  

可能重复:
  NSMutableURLRequest timeout doesn't trigger if data starts to load but not webViewDidFinishLoad

我正在使用Async HTTP请求并将我的NSMutableURLRequest的超时设置为30秒,我希望在此时间内超时请求。

问题:请求没有超时30秒。超时始终需要90秒。任何解决方案。

这是我的连接代码:

NSMutableURLRequest *myRequest = [[NSMutableURLRequest alloc] initWithURL:myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody:myData];

NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];

我正在实施以下指南:

- (void)connection:(NSURLConnection *)iConnection didReceiveResponse:(NSURLResponse *)iResponse;
- (void)connection:(NSURLConnection *)iConnection didReceiveData:(NSData *)iElementContructionContextData;
- (void)connection:(NSURLConnection *)iConnection didFailWithError:(NSError *)iError;

1 个答案:

答案 0 :(得分:5)

您可以设置timeOutSeconds的时间,如下所示: -

[myRequest setTimeOutSeconds:1000];

根据你自己设定timeOutSecond。