如何在 iPhone 中中止同步 URL 请求
我有一个 API 调用。需要2分多钟才能得到服务器的响应。我如何在处理时手动中止它?
这是我的代码:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval: 600.0];
[request setHTTPMethod:@"GET"];
NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
[headers setValue:@"application/json" forKey:@"Content-Type"];
[headers setValue:@"application/json" forKey:@"Accept"];
[headers setValue:val forKey:@"Authorization"];
[request setAllHTTPHeaderFields:headers];
NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
如何在处理请求时中止请求。
谢谢
I have an API call. It takes more than 2 minute to get a response from the server. how can i abort it manually while it is processing?
Here is my code:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval: 600.0];
[request setHTTPMethod:@"GET"];
NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
[headers setValue:@"application/json" forKey:@"Content-Type"];
[headers setValue:@"application/json" forKey:@"Accept"];
[headers setValue:val forKey:@"Authorization"];
[request setAllHTTPHeaderFields:headers];
NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
how can i abort the request while its processing.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。如果您希望能够取消,请使用异步连接。
另外,顺便说一句,您的代码中有一些错误:
Error
应该初始化为 nil。响应
。将其初始化为 nil,sendSynchronousRequest:returningResponse:error:
将填充它(无论如何,如果请求成功)。[[NSURLConnection alloc] initWithRequest:request delegate:self]
行启动异步连接并释放它,在您已经为同一请求创建了同步连接。并且它泄漏了 NSURLConnection 对象。您是否在您的 self 对象上实现了非正式委托协议的必要方法?You can't. Use asynchronous connections if you want to be able to cancel.
Also, BTW, you have some errors in your code:
Error
should be initialized to nil.response
. Initialize it to nil, andsendSynchronousRequest:returningResponse:error:
will fill it in (if the request succeeds, anyway).[[NSURLConnection alloc] initWithRequest:request delegate:self]
starts an asynchronous connection and releases it, after you already did a synchronous connection for the same request. And it leaks the NSURLConnection object. Did you even implement the necessary methods for the informal delegate protocol on yourself
object?你不能,因为线程被你的同步请求阻止。
http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
You can't, as the thread is being blocked by your synchronous request.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html