- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试连接到 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/
我收到此错误消息: .rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:24:in `' 我重新安装了 Ruby rvm reinstall ruby-2.5
我开始从事 WPF Ribbon 开发,非常好! 我的问题是找到(免费)基本图标(如文件保存/打开/等,剪切/粘贴/等)。 你有什么建议吗? 最佳答案 你看过Visual Studio Icon Li
我只找到经典的声音ID,但我需要Chord(默认)日历警报。如何播放声音? ks #define systemSoundID 1315 AudioServicesPlaySystemSound (s
在 Magento 中创建货件时,有一个复选框可让您“通过电子邮件发送货件副本”。 默认情况下未选中。有谁知道我需要编辑哪个文件才能默认设置为“选中”? 最佳答案 这是一个app/design/adm
我有一个简单的 IValueConverter,它只使用 TypeConverter 进行转换。但是,在某些情况下,提供的 TypeConverter 会失败。 如果转换器未提供 Binding,我想
我正在阅读教程,默认 Activity 是一个扩展另一个类的类,它所拥有的只是一个覆盖方法。应用程序如何工作,因为它不做任何其他事情?我很困惑! 最佳答案 父类 Activity 为您处理一切。 关于
我刚刚开始研究游戏框架。我正在尝试构建 rest api,并将 postgresql 用于我的数据库连接。这是我第一次同时使用 play 和 postgre。我在 build.sbt 中建立了一个数据
是否可以创建具有以下属性的 python 对象: class Foo: def __default_method__(x): return x f = Foo() f(10) > 10
我是 jQuery 的新手,遇到了一个烦人的问题。我有一些登录字段,当该字段为空时会填充默认文本,然后在单击时删除。 我的问题是,当用户保存了他们的用户名/密码(使用浏览器)时,如果他们返回页面,登录
考虑这个代码片段: void Foo(std::string str1, std::string str2) {} template void Bar() { Foo(Types{}...);
我正在编写一个简单的 C 程序,我应该用缓冲区溢出来攻击它。所以,我不想在编译时使用任何标志。如何消除使用的默认标志? # readelf -p .GCC.command.line stack Str
考虑这个代码片段: void Foo(std::string str1, std::string str2) {} template void Bar() { Foo(Types{}...);
我有以下代码[这是一道面试题]: #include #include using namespace std; class A{ public: A(){ cout co
我想在 Autofac 中为每个匹配的生命周期范围注册创建一个实例,但偶尔需要从全局容器(没有匹配的生命周期范围)请求一个实例。在不存在匹配生命周期范围的情况下,我想给出一个顶级实例而不是抛出异常。
我正在做一个收集单词共现的修改版本,所以我编写了自己的 javascript,我正在跟踪三个对象中的出现。但是,一旦对象变大(约 800 万、300 万和 172000),每 100000 个句子需要
我正在使用 pykalman 模块中的 KalmanFilter,我想知道它如何处理缺失的观察结果。根据文档: In real world systems, it is common to have
我有一个应用了 RenderTransform 的 Canvas ,如下所示: 谁能告诉我这些值是什么意思?我似乎无法找到用于解析这些值的转换器。 最佳答案 如 RenderTransform是 T
我是 Linux 的新手,现在使用 CentOS 6。我在这里使用 MySQL 工作台,每当我尝试添加新连接时,它都会询问我默认的 key 环密码。我真的不知道,这个密码是从哪里设置的,我之前没有设置
我在 Ubuntu 18.04 上工作。我没有定义 GL_GLEXT_PROTOTYPES .我使用 glXGetProcAddress 加载“核心”OpenGL 函数.我的申请链接到 /usr/li
我按照文档中的示例添加了对使用 asio 加载 HTTPS 站点的支持,这意味着我调用 ctx.set_default_verify_paths();使用系统默认路径来查找证书。 然而,我得到:una
我是一名优秀的程序员,十分优秀!