我需要检查网络服务器的状态,特别是网络服务器是否返回正确的状态代码 200 的信息。有没有办法从 iOS 中的网络服务器获取 HTTP 响应代码?
最佳答案
这在 official docs 中有详细记录,无需实现第 3 方库(尽管如果您不熟悉 iOS 编码,网络 API 可以简化底层函数调用)。你的类必须是一个 NSURLConnection 委托(delegate),并且在创建 NSURLConnection 之后你实现委托(delegate)方法是这样的:
-(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)
}
}
您应该找到 URL Loading Guide阅读此内容和 iOS 上的其他网络功能非常有用。
关于ios - 如何在 iOS 中检查 Web 服务器的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238377/