如何取消 Web 服务调用 - IOS
我的 iPhone 应用程序中有用户身份验证。用户登录的工作方式是应用程序将用户名/密码发送到 Web 服务以供用户检查。我现在遇到的问题是我不知道如何设置“超时”(30 秒)并停止从 Web 服务请求数据?
NSString *soapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>...", username, password
];
NSURL *url = [NSURL URLWithString: @"http://...Service.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://.../LoginUser" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
webData = [[NSMutableData data] retain];
}
I have user authentication in my iPhone application. User login works in a way that application send username/password to web service for user check. The problem I have now is I don't know how to setup 'timeout' (30 seconds) and stop requesting data from web service?
NSString *soapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>...", username, password
];
NSURL *url = [NSURL URLWithString: @"http://...Service.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://.../LoginUser" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
webData = [[NSMutableData data] retain];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 NSURLConnection 的取消方法
You can use the cancel method of the NSURLConnection
也许 ASIHTTP 可能对您有帮助?
另外,如果我理解正确的话,stackoverflow 上也有一个主要的类似问题。
Maybe ASIHTTP might help you?
Also, if I understood you correctly, a mostly similar issue on stackoverflow.