如何取消 Web 服务调用 - IOS

发布于 2024-11-25 11:12:24 字数 1006 浏览 1 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(3

唠甜嗑 2024-12-02 11:12:24
[req setTimeoutInterval:urInterval];
setTimeoutInterval:

设置接收器的超时间隔,以秒为单位。

- (void)setTimeoutInterval:(NSTimeInterval)timeoutInterval

参数

超时间隔

超时间隔,以秒为单位。如果在连接尝试期间

请求保持空闲状态的时间超过超时间隔,
请求被认为已超时。默认超时间隔
是 60 秒。

[req setTimeoutInterval:urInterval];
setTimeoutInterval:

Sets the receiver’s timeout interval, in seconds.

- (void)setTimeoutInterval:(NSTimeInterval)timeoutInterval

Parameters

timeoutInterval

The timeout interval, in seconds. If during a connection attempt

the request remains idle for longer than the timeout interval, the
request is considered to have timed out. The default timeout interval
is 60 seconds.

盛夏尉蓝 2024-12-02 11:12:24

您可以使用 NSURLConnection 的取消方法

/*! 
    @method cancel
    @abstract Cancels an asynchronous load of a URL.
    @discussion Once this method is called, the asynchronous load is
    stopped, and the NSURLConnectionDelegate associated with the
    receiver will no longer receive any asynchronous callbacks for
    this NSURLConnection.
*/
- (void)cancel;

You can use the cancel method of the NSURLConnection

/*! 
    @method cancel
    @abstract Cancels an asynchronous load of a URL.
    @discussion Once this method is called, the asynchronous load is
    stopped, and the NSURLConnectionDelegate associated with the
    receiver will no longer receive any asynchronous callbacks for
    this NSURLConnection.
*/
- (void)cancel;
时常饿 2024-12-02 11:12:24

也许 ASIHTTP 可能对您有帮助?

另外,如果我理解正确的话,stackoverflow 上也有一个主要的类似问题

Maybe ASIHTTP might help you?

Also, if I understood you correctly, a mostly similar issue on stackoverflow.

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