2011-07-22 34 views
0

我在我的iPhone应用程序中有用户身份验证。用户登录以应用程序将用户名/密码发送到Web服务进行用户检查的方式工作。我现在的问题是我不知道如何设置“超时”(30秒)并停止从Web服务请求数据?如何取消网络服务呼叫 - IOS

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]; 
    }  

回答

4
[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.

+0

我添加 '[REQ setTimeoutInterval:30];'。现在如果请求超时,这个处理程序在哪里?它是' - (void)连接:(NSURLConnection *)连接didFailWithError:(NSError *)错误'? – 1110

0

您可以使用取消的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; 
0

方法也许ASIHTTP可以帮助你?

另外,如果我正确地理解了你,在stackoverflow上主要是similar issue