2
AFHTTPRequestOperation을 사용하여 구문 분석 할 몇 가지 HTML 페이지를 얻으려고합니다. 내가 가지고있는 문제는 잘못된 포트 번호를 사용하여 서버에 연결할 때 '실패'오류 블록이 실행되지 않는다는 것입니다. 대신 완료 블록이 발생하지만 응답 문자열은 nil입니다. 난 그냥 ASIHTTP에서 AFNetworking로 전환 중이며 ASIHTTP를 사용하면 오류가 발생합니다.오류 대신 AFHTTPRequestOperation 무응답
확실히이 오류가 발생합니까? 이걸 어떻게 잡을 수 있니?
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];
결과 블록을 확인하는 성공 블록에 체크를 추가 할 수 있습니다 (nil인지 확인). AFHTTPRequestOperation을 서브 클래스 화하여 오류를 차단할 수도 있습니다. – Till
네, 사실 그럴 것입니다. 왜 내가 오류가 발생하지 않았는지 궁금 해서요, 내가 테스트 한 항구가 사파리에서 빈 페이지를 주었기 때문에 오히려 오류가 아닌 것 같아요. – Darren
예, 비어있는 결과는 HTTP 오류로 간주되지 않습니다. Tshirt가 있습니다.) – Till