ios FTP 使用 NSURLSession 上传
ios FTP upload using NSURLSession
我正在尝试通过 FTP 将文件上传到服务器。根据
Apple Documentation NSURLSession class 支持 FTP 操作。
有一个著名的 Apple Developer blog 也支持它。但是还不清楚NSURLSession API 是否支持ftp 上传? (我尝试了建议的方式并出现错误)。
使用 CFFTPStreamRef 的传统方式,ftp 上传工作正常,但在 9.0 中已弃用。 header 表示:CF_DEPRECATED(10_3, 10_11, 2_0, 9_0 , "Use NSURLSessionAPI for ftp requests")
任何想法、示例或 link 寻求帮助。我现在正在尝试这样的事情:
NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password@thelink/myfolder/filename.zip"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload];
[request setHTTPMethod:@"PUT"];
NSURL *docsDirURL = [NSURL fileURLWithPath:filePath];
NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url_upload.host port:[url_upload.port integerValue] protocol:url_upload.scheme realm:nil authenticationMethod:nil];
NSURLCredential *cred = [NSURLCredential
credentialWithUser:userId
password:password
persistence:NSURLCredentialPersistenceForSession];
NSURLCredentialStorage * cred_storage ;
[cred_storage setCredential:cred forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.URLCredentialStorage = cred_storage;
sessionConfig.timeoutIntervalForRequest = 30.0;
sessionConfig.timeoutIntervalForResource = 60.0;
sessionConfig.allowsCellularAccess = YES;
sessionConfig.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:docsDirURL];
[uploadTask resume];
据我所知,NSURLSession(和 NSURLConnection)支持通过 FTP 检索文件,但不支持任何其他 FTP 命令,例如 STOR(注意 PUT 是一个 HTTP方法,而不是 FTP 方法)。
就您的目的而言,您的选择是使用 CFFTPStream API(几乎不起作用)或停止使用 FTP.
我强烈建议停止使用 FTP。 FTP 协议非常不安全,通过网络以明文形式发送用户名和密码,这使得人们很容易嗅探凭据并伪装成用户。因此,如今 FTP 上传甚至可以远程接受的唯一情况是匿名 FTP 上传到共享保管箱,即便如此,它还是有些可疑。这就是为什么该功能从未添加到 NSURLConnection API,更不用说 NSURLSession.
有更好、更安全的替代方案,例如通过 HTTPS 的 WebDAV、通过 HTTPS 的 POST 请求上传、WebDAV 或带有摘要身份验证的 POST 请求等。这些替代方案是实际上支持 NSURLSession 并提供其他优势,如恢复下载的能力。除非您绝对无法更改服务器端,否则请改用其中一种替代方法。
我正在尝试通过 FTP 将文件上传到服务器。根据 Apple Documentation NSURLSession class 支持 FTP 操作。
有一个著名的 Apple Developer blog 也支持它。但是还不清楚NSURLSession API 是否支持ftp 上传? (我尝试了建议的方式并出现错误)。
使用 CFFTPStreamRef 的传统方式,ftp 上传工作正常,但在 9.0 中已弃用。 header 表示:CF_DEPRECATED(10_3, 10_11, 2_0, 9_0 , "Use NSURLSessionAPI for ftp requests")
任何想法、示例或 link 寻求帮助。我现在正在尝试这样的事情:
NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password@thelink/myfolder/filename.zip"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload];
[request setHTTPMethod:@"PUT"];
NSURL *docsDirURL = [NSURL fileURLWithPath:filePath];
NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url_upload.host port:[url_upload.port integerValue] protocol:url_upload.scheme realm:nil authenticationMethod:nil];
NSURLCredential *cred = [NSURLCredential
credentialWithUser:userId
password:password
persistence:NSURLCredentialPersistenceForSession];
NSURLCredentialStorage * cred_storage ;
[cred_storage setCredential:cred forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.URLCredentialStorage = cred_storage;
sessionConfig.timeoutIntervalForRequest = 30.0;
sessionConfig.timeoutIntervalForResource = 60.0;
sessionConfig.allowsCellularAccess = YES;
sessionConfig.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:docsDirURL];
[uploadTask resume];
据我所知,NSURLSession(和 NSURLConnection)支持通过 FTP 检索文件,但不支持任何其他 FTP 命令,例如 STOR(注意 PUT 是一个 HTTP方法,而不是 FTP 方法)。
就您的目的而言,您的选择是使用 CFFTPStream API(几乎不起作用)或停止使用 FTP.
我强烈建议停止使用 FTP。 FTP 协议非常不安全,通过网络以明文形式发送用户名和密码,这使得人们很容易嗅探凭据并伪装成用户。因此,如今 FTP 上传甚至可以远程接受的唯一情况是匿名 FTP 上传到共享保管箱,即便如此,它还是有些可疑。这就是为什么该功能从未添加到 NSURLConnection API,更不用说 NSURLSession.
有更好、更安全的替代方案,例如通过 HTTPS 的 WebDAV、通过 HTTPS 的 POST 请求上传、WebDAV 或带有摘要身份验证的 POST 请求等。这些替代方案是实际上支持 NSURLSession 并提供其他优势,如恢复下载的能力。除非您绝对无法更改服务器端,否则请改用其中一种替代方法。