【问题标题】:iPhone: Is it possible to open a password-protected file using openURL?iPhone:是否可以使用 openURL 打开受密码保护的文件?
【发布时间】:2010-02-22 17:45:57
【问题描述】:

标题几乎说明了一切。我的应用程序有文件 myFile.ext 的 URL 和密码,位于:

https://myserver.com/stuff.cgi?db=mydb

我想创建一个 NSURL 对象,如果将其传递给 UIApplication 的 canOpenURL 和 openURL 方法,将产生适当的行为。

这可能吗?如果有怎么办?还有我应该注意的安全问题吗?

编辑澄清:

以下代码会生成一个 URL 请求,该请求在发送到服务器时会成功导致应用下载文件。但我想做的是用 openURL 打开它。

+ (NSMutableURLRequest *) requestForFileNamed: (NSString *) filename {
    NSString *url = [NSString stringWithFormat:@"%@&user=%@&getbinfile=%@", serverLocation, username, filename];
    NSString *body = [NSString stringWithFormat:@"password=%@", password];
    return [XMLRequestBuilder postRequestWithURL:url body:body];
}

XMLRequestBuilder 方法:

+ (NSMutableURLRequest *) requestWithURL: (NSString *) url body: (NSString *) body method: (NSString *) method {
    NSURL * theURL = [NSURL URLWithString:url];
    NSMutableURLRequest * ret = [NSMutableURLRequest requestWithURL:theURL];
    [ret setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    [ret setHTTPMethod: method];
    [ret setTimeoutInterval:kDefaultTimeoutInterval];
    return ret;
}


+ (NSMutableURLRequest *) postRequestWithURL: (NSString *) url body: (NSString *) body {
    return [XMLRequestBuilder requestWithURL:url body:body method:@"POST"];
}

【问题讨论】:

  • 您是指文件本身受密码保护,还是 Web 资源通过 .htaccess 或类似文件受密码保护?
  • 我在这方面有点菜鸟。 . .我可以肯定地告诉你的是,以下请求将允许我下载文件。 URL 是 myserver.com/… body 是 password=mypassword 方法是 POST
  • 您能否发布创建 NSMutableURLRequest 的代码(在 XMLRequestBuilder 中)?

标签: iphone passwords openurl


【解决方案1】:

假设(正如@bodnarbm 指出的那样)您想要 HTTP 身份验证,这相当简单。只需实现 didReceiveAuthenticationChallenge。以下是 Apple 文档中的示例:

只需将 [self preferencesName] 和 [self preferencesPassword] 更改为您的用户名/密码。

-(void)connection:(NSURLConnection *)connection
        didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:[self preferencesName]
                                                 password:[self preferencesPassword]
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that the user name and password
        // in the preferences are incorrect
        [self showPreferencesCredentialsAreIncorrectPanel:self];
    }
}

这是链接:http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

更新: 从您上面的评论来看,您似乎没有使用 HTTP 身份验证(因此我上面的代码不适用于您,但我会将其留给其他人帮助)。

回到您的问题:您是否在请求中将 HTTP 方法标头值设置为“POST”?为什么您要尝试在正文中发送密码(作为 POST 应该)而其他参数在 URL 中(作为 GET)?将其他参数移动到 POST 请求的正文中。如果您发布代码,可能会更容易看出哪里出错了。

【讨论】:

  • 感谢您愿意看这个。我正在使用的系统正在切换身份验证方法,我刚刚意识到我正在使用旧系统获取文件。所以我最好等到这部分被切换后再发布我的代码。可能需要几天时间。我会在它准备好后发布。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-10-08
  • 2020-08-29
  • 1970-01-01
  • 2018-10-01
  • 2013-07-14
  • 2018-08-09
  • 2021-01-20
相关资源
最近更新 更多