2
我正在使用AFHTTPRequestOperation獲取一對HTML頁面進行解析。 我遇到的問題是,如果我使用錯誤的端口號,例如連接到服務器時,'失敗'錯誤塊不會觸發。相反,完成塊觸發,但響應字符串爲零。 我只是從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];
您可以在您的成功塊中添加一個支票來驗證結果數據(檢查是否爲零)。您也可以繼承AFHTTPRequestOperation來做到這一點,並跳轉到這些情況下的錯誤塊。 – Till
是的,這是真的,我可能會這樣做。我想知道爲什麼我沒有得到一個錯誤,降低了我測試的端口在safari中給了一個空白頁面,所以我想這畢竟不是一個錯誤。 – Darren
贊成,空結果不算HTTP錯誤 - 在那裏,得到了T恤;) – Till