我需要检查Web服务器的状态,特别是Web服务器返回正确的状态代码200的信息。有没有办法从iOS中的Web服务器获取HTTP响应代码?
答案 0 :(得分:2)
这在official docs中有详细记载,无需实现第三方库(尽管如果您是iOS编码的新手,网络API可以简化底层函数调用)。您的类必须是NSURLConnection委托,并且在进行NSURLConnection后,您实现了类似的委托方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse*)response
{
if ([response statusCode] == 200) {
// Handle valid response (You can probably just let the connection continue)
}
else {
// Other status code logic here (perhaps cancel the NSURLConnection and show error)
}
}
你应该在iOS上找到这个和其他网络功能的URL Loading Guide非常有用的读物。