菜鸟教程小白 发表于 2022-12-12 09:34:21

ios - 在iOS中解析JSON时是否可以检索描述以及状态码


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

<p>这是我在 Rest 客户端中得到的结果:</p>

<pre><code>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
</code></pre>

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

<p>提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>假设您使用 <code>NSURLConnection</code> 发送请求,您可以从响应中获取状态码:</p>

<pre><code>NSError* error = nil;
NSHTTPURLResponse* response = nil;

NSData* data = ;
int statusCode = response.statusCode;
NSString* statusText = ;
</code></pre>

<p>如果您不使用同步请求,则可以使用 <a href="https://stackoverflow.com/questions/6918760/nsurlconnectiondelegate-getting-http-status-codes" rel="noreferrer noopener nofollow">here</a> 中描述的委托(delegate)方法.</p>

<p>希望有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在iOS中解析JSON时是否可以检索描述以及状态码,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15378928/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15378928/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在iOS中解析JSON时是否可以检索描述以及状态码