3

我遇到了一些严重的问题[NSURLConnection sendAsynchronousRequest: queue: completionHandler:]。我正在使用休息代理来登录和验证用户。我用来登录的以下内容如下所示:

    //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 和一个空 JSON 对象。这应该返回error = nil一个状态码 401 和一个有效的非空响应对象。由于某种原因,返回错误并且响应为零:

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:]

4

2 回答 2

1

2xx当 URL 处理系统收到错误 HTTP 状态代码(即 not )时,不要依赖于没有生成错误。

相反,使用返回的response,它可以让您访问 HTTP 状态代码(在运行时检查类并强制转换为,NSHTTPURLResponse以便您可以访问statusCode)。

于 2013-11-06T22:35:33.757 回答
0

原来是我们后端服务器中的一个错误。有时它会发送非常奇怪的响应代码,这些代码被 iOSNSURLConnection库处理不当。在这种特殊情况下,它显示为域错误。

于 2014-02-25T03:36:05.470 回答