NSURLConnection 返回错误而不是 401 的响应 [英] NSURLConnection returning error instead of response for 401

查看:50
本文介绍了NSURLConnection 返回错误而不是 401 的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web API,对于特定请求,如果一切正常,则返回状态代码 200,如果用户未基于授权令牌登录,则返回状态代码 401.如果响应状态为 200,则一切正常,但如果响应状态为 401,则似乎无法正常工作,返回代码为 -1012 的连接错误,而响应为零.

I have a web API that, for a specific request returns status code 200 if everything went ok, and 401 if the user is not logged in based on an Authorization token. Everything works fine if the response status is 200, but doesn't seem to work properly if the response status is 401, returning a connection error with code -1012, while the response is nil.

所以,下面的代码:

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"%@", response);
    NSLog(@"%@", connectionError);

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    int statusCode = (int)[httpResponse statusCode];
    NSLog(@"response status code: %d", statusCode);

将显示

2015-04-01 15:58:18.511 MyProject[3618:694604] <NSHTTPURLResponse: 0x155facc0> { URL: *SOME_URL* } { status code: 200, headers {
    "Access-Control-Allow-Headers" = "Content-Type, Accept, X-Requested-With";
    "Access-Control-Allow-Methods" = "POST, GET, PUT, UPDATE, OPTIONS";
    "Access-Control-Allow-Origin" = "*";
    Connection = "keep-alive";
    "Content-Type" = "application/json";
    Date = "Wed, 01 Apr 2015 12:58:14 GMT";
    Server = "Wildfly 8";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "Undertow 1";
} }
2015-04-01 15:58:18.513 MyProject[3618:694604] (null)
2015-04-01 15:58:18.513 MyProject[3618:694604] response status code: 200

如果响应状态是200,而如果状态码是401,我会得到:

if the response status is 200, while if the status code is 401, I will get:

2015-04-01 16:05:55.988 MyProject[3633:695836] (null)
2015-04-01 16:05:55.992 MyProject[3633:695836] Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x146137c0 {NSErrorFailingURLKey=*SOME_URL*, NSErrorFailingURLStringKey=*SOME_URL*, NSUnderlyingError=0x1459e6d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
2015-04-01 16:05:55.992 MyProject[3633:695836] response status code: 0

如果我使用 Postman 或 Android 设备执行相同的请求,我将获得带有以下标头的状态代码 401(从 Postman 复制):

If I do the same request using Postman or an Android device, I will get status code 401 with the following headers(copied from Postman):

Connection → keep-alive
Content-Length → 30
Content-Type → application/json
Date → Wed, 01 Apr 2015 13:07:34 GMT
Server → Wildfly 8
X-Powered-By → Undertow 1

是否有任何修复程序或库可以为我提供一些准确的响应状态?我搜索了一些关于 -1012 错误的信息,但找不到太多内容,我真的不想以此为基础.

Is there any fix or maybe a library that could give me some accurate response status? I searched a bit about the -1012 error, but couldn't find much and I don't really want to base on that.

经过一番研究,我在 Appl 的文档中发现了以下声明:如果需要身份验证才能下载请求,则必须将所需的凭据指定为 URL 的一部分.如果身份验证失败,或凭据丢失,连接将尝试在没有凭据的情况下继续."

after a bit of research I found the following statement on Appl's documentation: "If authentication is required in order to download the request, the required credentials must be specified as part of the URL. If authentication fails, or credentials are missing, the connection will attempt to continue without credentials."

但是我怎么知道这个错误是否会在 401 状态之后呢?它可以出现在其他类型的请求之后吗?

But then how can I know if this error will be after a 401 status? Can it appear after another type of request?

推荐答案

为了得到 401 状态码,我认为你需要实现协议 NSURLConnectionDelegate然后 connection:didReceiveAuthenticationChallenge:.

In order to get the 401 status code, I think you'll need to implement protocol NSURLConnectionDelegate and then connection:didReceiveAuthenticationChallenge:.

因此,您还需要传递委托,可能使用 [NSURLConnection connectionWithRequest:request delegate:self].

So, you'll also need to pass the delegate, maybe using [NSURLConnection connectionWithRequest:request delegate:self].

而且,如果您不尝试实施身份验证挑战,我宁愿始终返回 200 状态代码,但具有不同的 json 内容.

And, if you aren't trying to implement the authentication challenge, I would rather always return the 200 status code, but with different json content.

希望能帮到你.

这篇关于NSURLConnection 返回错误而不是 401 的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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