gpt4 book ai didi

ios - NSURLConnection 不使用默认凭据

转载 作者:可可西里 更新时间:2023-11-01 05:04:51 24 4
gpt4 key购买 nike

我正在尝试连接到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com