ios - 如何快速从 HTTP Get 获取状态码?

标签 ios http swift

我已尝试使用以下代码在 IOS Swift 中实现 HTTP Get。

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler: {(response: NSURLResponse!, data: NSData!,error: NSError!) -> Void in

            var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
            println("response = \(response)")
        })

它成功并显示以下响应:

response = <NSHTTPURLResponse: 0x155xxxxx> { URL: http://xxx.xx.xxx.xxx:38080/getid/user-wiced1 } 
{ status code: 200, headers {
    "Content-Length" = 111;
    "Content-Type" = "application/json; charset=UTF-8";
    Date = "Thu, 08 Jan 2015 07:31:21 GMT";
    Server = "spray-can/1.3.2-20140909";
} }

如何从 NSURLResponse 获取状态码

提前致谢。

最佳答案

NSURLResponse 没有状态码,但是 NSHTTPURLResponse 有。所以投它:

let httpResponse = response as NSHTTPURLResponse
httpResponse.statusCode

关于ios - 如何快速从 HTTP Get 获取状态码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27835398/

相关文章:

ios - swift - UIImageWriteToSavedPhotosAlbum 导致 EXC_BAD_ACCESS

android - 如何在不丢失 css 的情况下将模板从桌面发送到移动设备

http - 谷歌翻译 API v2 : Allowing certain HTTP referrers

导航栏中的快速粗体标题

ios - UIImage(文件内容:) returning nil despite file existing in caches directory

ios - 如何在 Swift 中获取图像文件大小?

javascript - 使用 Angular.js 查找正在进行的 http get 请求的下载状态

python - 示例 urllib3 和 python 中的线程

ios - 使用 Swift 动画循环进度

ios - 在两个圆形 UIBezierPaths 之间设置动画时的奇怪行为