首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLConnection验证

NSURLConnection验证
EN

Stack Overflow用户
提问于 2010-08-17 02:47:32
回答 2查看 2.7K关注 0票数 2

我正在尝试验证连接是否成功,但得到的结果不稳定。当我尝试使用伪造的url执行同步请求时:

代码语言:javascript
复制
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (responseData)   
    {  
        did_send = TRUE;  
    }   
    else   
    {  
        did_send = FALSE;
    }

它挂起了一段时间,并最终返回:

代码语言:javascript
复制
 did_send = FALSE;

但是如果我使用一个伪造的url进行异步请求:

代码语言:javascript
复制
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self ];
if (conn)   
    {  
        did_send = TRUE;  
    }   
    else   
    {  
        did_send = FALSE;
    }

我得到了:

代码语言:javascript
复制
did_send = TRUE;

每次都是这样。我需要让异步请求工作,因为我能够设置超时,并且在请求超时时不需要挂起60秒,而默认的超时持续时间对于异步请求是不可更改的。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-17 02:55:09

尝试使用nsurlconnection类的委托方法connection:didFailWithError:,这样可以得到一致的结果。

代码语言:javascript
复制
-(void)getLocationsFromWebService {
    NSLog(@"in getLocationsFromWebService");


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];


    NSURLRequest *theRequest = [NSURLRequest requestWithURL:self.locationsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    self.requestType = @"GET";
    if (theConnection) {
        _responseData = [[NSMutableData data] retain ];
    } else {

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; 
    NSLog(@"in mapviewcontroller");
    NSLog(@"Error connecting - %@",[error localizedDescription]);
    [connection release];
    [_responseData release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else {
        if ([self.requestType isEqualToString:@"GET"])
            [_responseData setLength:0];
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if ([self.requestType isEqualToString:@"GET"])
        [_responseData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

    if ([self.requestType isEqualToString:@"GET"]) {
        [self parseLocations:_responseData];
        [_responseData release];
    }
    [connection release];

}
票数 7
EN

Stack Overflow用户

发布于 2010-08-17 05:02:39

简单地实例化NSURLConnection不会做任何事情。它创建该类的一个实例,但不开始数据传输--即使请求不可能得到满足,对象通常也是非空的。

相反,委托对象(代码中的self)需要实现NSURLConnectionDelegate (它将处理诸如“传入数据”或“错误条件”之类的回调)。然后,您必须向NSURLConnection实例发送-start消息,该消息将在正在执行的线程的run循环中调度它。

有关NSURLConnectionDelegate协议的更多信息(包括如何在回调中执行的信息),请参阅苹果的“URL Loading System Programming Guide”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3496245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档