Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSURLConnection,NSURLAuthenticationChallenge失败响应_Ios_Nsurlconnection - Fatal编程技术网

Ios NSURLConnection,NSURLAuthenticationChallenge失败响应

Ios NSURLConnection,NSURLAuthenticationChallenge失败响应,ios,nsurlconnection,Ios,Nsurlconnection,当尝试使用iOS7登录我的应用程序时,我得到了一个带有一些说明的失败响应。这是NSURLAuthenticationChallenge failureResponse的NSLog: <NSHTTPURLResponse: 0xa383570> { URL: <URL> } { status code: 401,headers { "Content-Length" = 1385;"Content-Type" = "text/html;charset=utf-8"; Dat

当尝试使用iOS7登录我的应用程序时,我得到了一个带有一些说明的失败响应。这是NSURLAuthenticationChallenge failureResponse的NSLog:

<NSHTTPURLResponse: 0xa383570> { URL: <URL> } { status code: 401,headers {
"Content-Length" = 1385;"Content-Type" = "text/html;charset=utf-8"; 
Date = "Fri, 13 Jun 2014 12:14:24 GMT";  
Server = "Apache-Coyote/1.1";  
"Www-Authenticate" = "Basic realm=\"booo\"";  
"x-errorCode" = 02;  
"x-errorDescription" = "com.idealination.core.server.client.http.ServerException:
Request failed: <URL> Status: CLIENT_ERROR_UNAUTHORIZED  
Error: #Unauthorized :: Invalid userid or password.";
} }    
{URL:}{状态代码:401,标题{
“内容长度”=1385;“内容类型”=“文本/html;字符集=utf-8”;
日期=“2014年6月13日星期五12:14:24 GMT”;
Server=“apachecoyote/1.1”;
“Www-Authenticate”=“基本领域”=“booo\”;
“x-错误代码”=02;
“x-errorDescription”=“com.idealination.core.server.client.http.ServerException:
请求失败:状态:客户端\u错误\u未授权
错误:#未经授权::无效的用户ID或密码。“;
} }    
我需要最后一行来知道我的错误是什么。但当我使用iOS6和iPhone 3gs时,我只得到:

<NSHTTPURLResponse: 0x1c507ae0> 


我应该怎么做才能得到像使用iOS7这样的响应?为什么我会得到不同的响应?

您不应该查找最后一行,您应该查找HTTP状态码,在本例中是401

if(urlResponse.statusCode == 401) { }
如果需要将其转换为状态代码作为字符串的含义,请使用

NSString *status = [NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode];

如果我的用户被挂起,但最后一行不同,我会得到相同的代码。这就是我需要这条线的原因。也许我在错误的地方捕捉到了服务器异常?但我在别的地方都抓不到。或者我做错了。@Roo:如果你按照上面的建议,你可以在
status
变量中读取你的“最后一行”…希望…我得到的状态是:Unauthorized。但我需要详细信息,为什么它是未经授权的。请尝试记录
urresponse.headerFields
——这是一个NSDictionary,您可以在其中找到您需要的内容。通常情况下,对特定错误消息进行字符串比较仍然是一种不好的做法,因为如果服务器配置发生更改,这种情况可能会改变。对于不同的“未授权”场景,最好使用不同的HTTP状态代码或具有更具体错误代码的不同自定义头。