我正在使用 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];