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

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

问题描述

我试图通过FTP上传文件到服务器。根据
Apple文档 NSURLSession类支持FTP操作。

还有一个着名的 Apple开发者博客,它也是支持。但是还不清楚NSURLSession API是否支持ftp上传? (我尝试了建议的方式并出现错误)。



使用传统方法 CFFTPStreamRef ,ftp上传可以正常工作,但在9.0中不推荐使用。头文件说: CF_DEPRECATED(10_3,10_11,2_0,9_0,使用NSURLSessionAPI进行ftp请求)

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

  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,POST请求通过HTTPS,WebDAV或POST请求进行上传,并使用摘要式身份验证等。这些备选方案实际上可以通过NSURLSession进行支持,并提供其他优势,例如恢复下载的能力。除非您完全没有办法更改服务器端,否则请使用其中一种替代方法。


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

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

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];

解决方案

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

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

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.

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天全站免登陆