OGeek|极客世界-中国程序员成长平台

标题: ios - 在iOS中解析JSON时是否可以检索描述以及状态码 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:34
标题: ios - 在iOS中解析JSON时是否可以检索描述以及状态码

在我的 iPhone 应用程序中,我正在使用 JSON 进行一些 Web 服务调用。在返回数据中,我得到一个状态码(200、404 等),以及它们正在发送的消息。

这是我在 Rest 客户端中得到的结果:

Status Code: 401 Invalid Access token
Cache-Control: private
Connection: Keep-Alive, Proxy-Support
Content-Length: 0
Date: Wed, 13 Mar 2013 06:37:26 GMT
Proxy-Support: Session-Based-Authentication
Server: Microsoft-IIS/7.5
Via: 1.1 WIN-JO3AMACI965
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

在此,我想收到“无效访问 token ”消息以及状态码 401。有可能吗?

提前致谢。



Best Answer-推荐答案


假设您使用 NSURLConnection 发送请求,您可以从响应中获取状态码:

NSError* error = nil;
NSHTTPURLResponse* response = nil;

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
int statusCode = response.statusCode;
NSString* statusText = [NSHTTPURLResponse localizedStringForStatusCode:statusCode];

如果您不使用同步请求,则可以使用 here 中描述的委托(delegate)方法.

希望有所帮助。

关于ios - 在iOS中解析JSON时是否可以检索描述以及状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378928/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4