我正在使用AFHTTPRequestOperation来解析几个HTML页面。 我遇到的问题是,如果我使用错误的端口号,例如,当连接到服务器时,“失败的”错误块不会触发。相反,完成块会触发,但响应字符串为零。 我只是从ASIHTTP切换到AFNetworking,而使用ASIHTTP会产生错误。
当然这应该提供错误?我怎么能陷阱呢?
NSString *urlString;
urlString = [NSString stringWithFormat:@"%@://%@:%@/",kHttp,_IP,_Port];
NSURL *url = [NSURL URLWithString:urlString];
// Set authorization
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithUsername:_User password:_Pass];
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"Info.htm" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([self parseInfoLive:operation.responseString])
_count_parsed++;
if (_count_parsed == 2)
[[NSNotificationCenter defaultCenter] postNotificationName:@"downloadsComplete" object:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error during connection: %@",error.description);
[[NSNotificationCenter defaultCenter] postNotificationName:@"errorConnecting" object:nil];
}];
[operation start];