ios FTP上传使用NSURLSession [英] ios FTP upload using NSURLSession

查看:41
本文介绍了ios FTP上传使用NSURLSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 FTP 将文件上传到服务器.根据Apple 文档 NSURLSession 类支持 FTP 操作.

I am trying to upload files via FTP to server. According to Apple Documentation NSURLSession class supports FTP operations.

有一个著名的 Apple Developer blog 也支持这一点.但是仍然不清楚 NSURLSession API 是否支持 ftp 上传?(我尝试了建议的方式并得到错误).

There is a famous Apple Developer blog which also supports that. But still its not clear whether NSURLSession API's supports ftp upload or not? (I tried with the way suggested and getting error).

使用 CFFTPStreamRef 的传统方式,ftp 上传工作正常,但已弃用在 9.0.标头说:CF_DEPRECATED(10_3, 10_11, 2_0, 9_0, "Use NSURLSessionAPI for ftp requests")

With the conventional way of CFFTPStreamRef, ftp upload works fine but it's deprecated in 9.0. The header says: CF_DEPRECATED(10_3, 10_11, 2_0, 9_0 , "Use NSURLSessionAPI for ftp requests")

获得帮助的任何想法、示例或链接.我现在正在尝试这样的事情:

Any idea, example or link to get help with. I am trying something like this for now:

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 方法).

To the best of my recollection, NSURLSession (and NSURLConnection) support retrieving files via FTP, but do not support any other FTP commands, such as STOR (note that PUT is an HTTP method, not an FTP method).

出于您的目的,您的选择是使用 CFFTPStream API(只能勉强工作)或停止使用 FTP.

For your purposes, your options are to either use the CFFTPStream API (which only barely works) or stop using FTP.

我强烈建议停止使用 FTP.FTP 协议非常不安全,通过网络以明文形式发送用户名和密码,这使得人们很容易嗅探凭据并伪装成用户.因此,如今唯一可以远程接受 FTP 上传的情况是匿名 FTP 上传到共享保管箱,即便如此,它还是有点可疑.这就是为什么该功能从未添加到 NSURLConnection API,更不用说 NSURLSession.

My strong recommendation would be to stop using FTP. The FTP protocol is hopelessly insecure, sending the username and password in cleartext form over the wire, which makes it really easy for people to sniff the credentials and masquerade as the user. So the only situation where an FTP upload would be even remotely acceptable these days is an anonymous FTP upload to a shared dropbox, and even then, it is somewhat dubious. That's why the functionality was never added to the NSURLConnection API, much less NSURLSession.

有更好的替代方案更安全,例如基于 HTTPS 的 WebDAV、基于 HTTPS 的 POST 请求上传、带有摘要身份验证的 WebDAV 或 POST 请求等.这些替代方案实际上可以被 NSURLSession 支持,并提供其他优势,例如恢复下载的能力.除非您绝对无法更改服务器端,否则请改用其中一种替代方法.

There are much better alternatives that are much more secure, such as WebDAV over HTTPS, POST request uploads over HTTPS, WebDAV or POST requests with digest authentication, etc. And those alternatives are actually supportable with NSURLSession and provide other advantages like the ability to resume downloads. Unless you have absolutely no way to change the server side, please use one of those alternatives instead.

这篇关于ios FTP上传使用NSURLSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆