ios - NSURLConnection 不使用默认凭据

标签 ios nsurlconnection nsurlcredential

我正在尝试连接到 http://cmis.demo.nuxeo.org/nuxeo/atom/cmis/使用 NSURLConnection。此演示 Web 服务是 documented要求身份验证(登录名:Administrator/密码:Administrator)。

编辑:此 Web 服务现在发送身份验证质询,而不是在提出问题时。

此网络服务发送身份验证质询,因此我无法使用connection:didReceiveAuthenticationChallenge: 委托(delegate)方法。相反,我在共享凭证存储中设置了一个默认的 NSURLCredential。不幸的是,NSURLConnection 没有使用这个默认凭证。

这是我的代码(使用 ARC,在 iOS 5 上测试):

@implementation ViewController
{
    NSMutableData *responseData;
}

- (IBAction) connect:(id)sender
{
    NSString *user = @"Administrator";
    NSString *password = @"Administrator";
    NSURL *nuxeoURL = [NSURL URLWithString:@"http://cmis.demo.nuxeo.org/nuxeo/atom/cmis/"];

    NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:password persistence:NSURLCredentialPersistenceForSession];
    NSString *host = [nuxeoURL host];
    NSNumber *port = [nuxeoURL port];
    NSString *protocol = [nuxeoURL scheme];
    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:host port:[port integerValue] protocol:protocol realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nuxeoURL];
    BOOL manuallyAddAuthorizationHeader = NO;
    if (manuallyAddAuthorizationHeader)
    {
        NSData *authoritazion = [[NSString stringWithFormat:@"%@:%@", user, password] dataUsingEncoding:NSUTF8StringEncoding];
        NSString *basic = [NSString stringWithFormat:@"Basic %@", [authoritazion performSelector:@selector(base64Encoding)]];
        [request setValue:basic forHTTPHeaderField:@"Authorization"];
    }
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    responseData = [NSMutableData data];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading:%@", connection);
    NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding]);
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"connection:%@ didFailWithError:%@", connection, error);
}

@end

由于未使用默认凭据,我收到的是 html(登录页面)而不是 xml。如果我将 manuallyAddAuthorizationHeader 变量设置为 YES,则授权有效并且我收到 xml。

我的问题是:为什么 NSURLConnection 不会自动使用默认的 NSURLCredential

最佳答案

在不进行身份验证质询的情况下发送默认凭据被认为是不安全的,尤其是在您通过纯 HTTP 进行通信的情况下。 HTTP 规范规定您可以主动发送凭据,但您通常应该等待身份验证质询。这就是 Cocoa 默认提供的行为。

如果您需要主动发送凭据,则必须自己手动添加 header 。您可以自己对其进行 base-64 编码,或者您可以使用 CFHTTP 函数为您完成编码,如下所示:

CFHTTPMessageRef dummyRequest =
    CFHTTPMessageCreateRequest(
        kCFAllocatorDefault,
        CFSTR("GET"),
        (CFURLRef)[urlRequest URL],
        kCFHTTPVersion1_1);
CFHTTPMessageAddAuthentication(
    dummyRequest,
    nil,
    (CFStringRef)username,
    (CFStringRef)password,
    kCFHTTPAuthenticationSchemeBasic,
    FALSE);
authorizationString =
    (NSString *)CFHTTPMessageCopyHeaderFieldValue(
        dummyRequest,
        CFSTR("Authorization"));
CFRelease(dummyRequest);

然后只需将 authorizationString 设置为 NSURLRequest 中的 Authorization header 即可。

(明目张胆复制自 https://stackoverflow.com/a/509480/100478 的代码,虽然我自己写过很多次类似的代码。)

关于ios - NSURLConnection 不使用默认凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9379258/

相关文章:

iOS 如何为给定服务器重置 NSURLCredential 或让我的应用程序忘记它曾经与该服务器交谈过?

ios - 调用 didReceiveChallenge 方法时应用程序卡住

ios - iOS swift 中 UIDocumentPickerController 的 Doc 文件问题?

ios - 哪个 PHAssetCollection 用于保存图像?

ios - Swift: 'ViewController.Type' 没有名为 'URL' 的成员

php - 将 json 字典数组从 swift 3 发送到 php

ios - Xcode 5可以打开我的项目,但是Xcode 6在尝试打开时崩溃

objective-c - 在 tabBarView 中管理套接字流

iOS POST 不适用于 HTTPS

ios - NSURLConnection 在 HTTP 401 后不会调用 NSURLConnectionDelegate 身份验证方法