如何在iOS中检查Web服务器的状态? [英] How to check status of web server in iOS?

查看:58
本文介绍了如何在iOS中检查Web服务器的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查Web服务器的状态,尤其是Web服务器返回正确的状态码200时的信息.有什么方法可以从iOS中的Web服务器获取HTTP响应代码吗?

I need to check status of web server, specifically, the information if the web server returns proper status code 200. Is there any way to get the HTTP response code from web server in iOS?

推荐答案

This is well documented in the official docs, with no need to implement a 3rd party library (although if you're new to iOS coding, a Networking API can simplify the underlying function calls). Your class must be an NSURLConnection delegate, and after making the NSURLConnection you implement the delegate method something like this:

-(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加载指南对于iOS上的此功能和其他联网功能非常有用.

You should find the URL Loading Guide very useful reading for this and other networking functions on iOS.

这篇关于如何在iOS中检查Web服务器的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆