【发布时间】: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 中)?