如何检查iOS中的Web服务器状态?

时间:2012-09-02 17:53:11

标签: ios http

我需要检查Web服务器的状态,特别是Web服务器返回正确的状态代码200的信息。有没有办法从iOS中的Web服务器获取HTTP响应代码?

1 个答案:

答案 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非常有用的读物​​。