如何检查iOS中的Web服务器的状态?
我需要检查Web服务器的状态,具体来说,如果Web服务器返回正确的状态代码200的信息。有什么办法从iOS的Web服务器获取HTTP响应代码?
这在官方文档中有详细logging ,不需要实现第三方库(尽pipe如果你是iOS编程的新手,一个networkingAPI可以简化底层的函数调用)。 你的类必须是一个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上findURL加载指南非常有用的阅读这个和其他networkingfunction。
- 检查一个URL是否有http://前缀
- iOS9不会从安全页面(SSL / HTTPS)加载不安全的资源
- 使stringWithContentsOfURLasynchronous – 是否安全?
- 我可以从NSHTTPURLResponse中的HTTP状态行访问“Reason Phrase”(原因短语)吗?
- 使用AFNetworking将图像上传到服务器达到了损坏
- 应用程序架构,当'状态改变'APNS失败
- Xcode 7.1在iOS 9中NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)
- ASIFormDataRequest POST请求问题
- 志愿者匹配API Objective C