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

发布于 2024-11-15 10:30:12 字数 1087 浏览 4 评论 0原文

可能的重复:
如果有数据,则 NSMutableURLRequest 超时不会触发开始加载但未加载 webViewDidFinishLoad

我正在使用异步 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;

Possible Duplicate:
NSMutableURLRequest timeout doesn't trigger if data starts to load but not webViewDidFinishLoad

I am using a Async HTTP request and setting timeout for my NSMutableURLRequest to 30 seconds and I want to timeout the request within this time.

Problem: Request is not getting timed out withing 30 seconds. It is always taking 90 seconds to timeout. Any solution for this.

Here is my code for connection:

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

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

And I am implementing following delgates:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-11-22 10:30:12

您可以像这样设置 timeOutSeconds 的时间:-

[myRequest setTimeOutSeconds:1000];

根据自己的情况设置 timeOutSecond。

You can set your time for timeOutSeconds like this:-

[myRequest setTimeOutSeconds:1000];

Set timeOutSecond according to yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文