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

标签 ios http

我需要检查网络服务器的状态,特别是网络服务器是否返回正确的状态代码 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/

相关文章:

android - 接受自签名 SSL 证书-> 在哪里设置默认的 TrustManager

angularjs - 发出 http 请求时删除内容类型 header

excel - 如何在后端使用电子表格将数据从 EXCEL 用户表单发布到 Google 表单?

ios - 使用 Objective C 以编程方式进行约束

ios: NSMutableArray 计数在添加对象后打印零?

ios - Restkit 创建重复项(错误 : "Managed object cache returned 2 objects") only on device, 不在模拟器中

iOS 12 更积极地关闭我的应用程序?

iphone - openfire 是否实现了 xmpp XEP-0234 Jingle 文件传输?

php - 我应该为多语言网站设置哪些 http header ?

javascript - 服务器返回空响应