[NSURLConnection sendAsynchronousRequest: queue: completionHandler:]
我遇到了一些严重问题。我正在使用rest代理登录并验证用户身份。我用于登录的以下内容如下所示:
//Display the progress HUB
[SVProgressHUD showWithStatus:@"Signing Up..." maskType:SVProgressHUDMaskTypeClear];
NSError *error;
[self setUserCredentials];
//assign the parentView controller to the sender and email to the local class variables
parentView = sender;
userEmail = email;
// create json object for a users session
NSDictionary* session = [NSDictionary dictionaryWithObjectsAndKeys:
email, @"email",
password, @"password",
[NSDictionary dictionaryWithObjectsAndKeys:
[[UIDevice currentDevice] model], @"device",
[[UIDevice currentDevice] identifierForVendor].UUIDString, @"device_id",
@"APNS", @"push_to",
[[NSUserDefaults standardUserDefaults] stringForKey:@"push_id"], @"push_id",
nil],
@"mession",
nil];
NSData *jsonSession = [NSJSONSerialization dataWithJSONObject:session options:NSJSONWritingPrettyPrinted error:&error];
NSString *url = [NSString stringWithFormat:@"%@api/v1/messions.json", CoffeeURL];
NSLog(@"URL: %@", url);
NSURL *URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonSession length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonSession];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"Length: %d", data.length);
if (error == nil)
{
//if no error ocours set the user defualts and create the mession and log into the app
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *JSONResponse = [NSJSONSerialization JSONObjectWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error: &error];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"JSON: %@", JSONResponse);
NSLog(@"Response: %d", httpResponse.statusCode);
if (JSONResponse != nil && httpResponse.statusCode == 200)
{
[self setUserDefualts:password sessionToUse:JSONResponse deckController:viewDeck sender:sender];
}
[SVProgressHUD dismiss];
}
else
{
NSLog(@"Error: %@", error);
//Dismiss progress HUB here and inform user that an internal error has occured
[SVProgressHUD dismiss];
NSString *errorString = @"Sorry, an error has occoured while connecting to the Coffee server.";
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
}];
当我使用有效的用户凭据登录时,此代码可以正常运行。当我使用无效凭据时,服务器会向iOS客户端发送401和null JSON对象。这应返回error = nil
和状态代码401,其中包含有效的非null响应对象。由于某种原因,返回错误并且响应为零:
Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed.
(NSURLErrorDomain error -1012.)" UserInfo=0x1cd53b50
{NSErrorFailingURLKey=http://192.155.94.183/api/v1/messions.json,
NSErrorFailingURLStringKey=http://192.155.94.183/api/v1/messions.json,
NSUnderlyingError=0x1cdc2b10 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork
error -1012.)"}
这使得我很难区分发送请求时发生的错误和服务器在用户的凭据与后端不匹配时生成的401错误。当我尝试使用:
创建和处理请求时NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
和NSURLDelegate:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Called Outside!");
NSDictionary *jsonSession;
NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
NSLog(@"Succeeded! Received %d bytes of data", [responseData length]);
recievedResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *err = nil;
jsonSession = [NSJSONSerialization JSONObjectWithData:[recievedResponse dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
if ([connection.originalRequest.URL isEqual:[NSURL URLWithString:[NSString stringWithFormat:@"%@api/v1/messions.json", CoffeeURL]]])
{
NSLog(@"Status: %d", httpResponse.statusCode);
NSLog(@"JSON: %@", jsonSession);
NSLog(@"Called Inside!");
}
}
当用户输入无效凭据时,返回的响应有效,不会引发错误,状态代码等于401。为什么这适用于(void)connectionDidFinishLoading:(NSURLConnection *)connection
但不适用于[NSURLConnection sendAsynchronousRequest: queue: completionHandler:]
?
答案 0 :(得分:1)
当URL处理系统收到错误的HTTP状态代码(即不是2xx
)时,不要依赖于未生成的错误。
相反,使用返回的response
,可以访问HTTP状态代码(在运行时检查类并转换为NSHTTPURLResponse
,以便访问statusCode
)。
答案 1 :(得分:0)
事实证明这是我们后端服务器中的一个错误。有时它会发送非常奇怪的响应代码,这些代码由iOS NSURLConnection
库不正确地处理。在这种特殊情况下,它显示为域错误。