如何在后台下载FTPES密码保护的文件

时间:2018-08-23 12:40:06

标签: c# unity3d

在过去的几周里,我一直在搞很多事情,似乎找不到正确的方法。 我在Unity中进行此操作,但是本质上应该可以在环境中维护。

我在FTPES上有3个音频文件和一个文本文档,这很明显,通过FTP的显式TLS:

我已成功使用以下代码登录以验证文件的存在:

WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
ftpRequest = (FtpWebRequest)WebRequest.Create("ftps://" + IP + "/" + fileEndPath);
ftpRequest.Credentials = new NetworkCredential(User, Password);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
ServicePointManager.Expect100Continue = true;

ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ValidConnection = true;[/code]

话虽如此,我也已经通过多种方式成功下载了文件。.我遇到的问题如下:

  • 使用FtpWebRequest我可以下载文件,并且以前能够将文件作为AudioClip加载(老实说,我不知道为什么它随机停止工作,使用ftpwebrequest进行下载是...)类,但是因为我似乎找不到找到将其推入后台的方法。.我尝试使用streamWriter写入文件,但似乎没有用。

  • 使用UnityWebRequest我无法登录FTPS服务器,因为它是为基于HTTP的通信而设计的。

  • 使用WebClient,我能够登录到服务器(通过注册前缀,如上所示,并将其与useSsl bool设置为true的低级别关联),

    • 使用.DownloadFile无法下载文件,挂起Unity Main Thread。
    • 使用.DownloadFileAsyn能够下载文件,但没有读取权限,并且从不会调用Complete事件来继续使用WWW加载文件。

截至本文发布地点,我正在使用WebClient.DownloadFileAsyn:

internal void Download(FileType fileType)
{
    IsDownloading = true;
    if (ValidConnection)
    {
        try
        {
            StartCoroutine(SetFileCreation());

            using (WebClient client = new WebClient())
            {
                WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
                client.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadFileProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
                client.DownloadFileAsync(ftpIP, Application.dataPath + "/StreamingAssets/" + ftpFilePath, fileType);
            }
        }
        catch (WebException ex)
        {
            Debug.LogError(ex.Message + ex.StackTrace + "\n");
        }
    }
    else
        IsDownloading = false;
}

SetFileCreation();是一种验证位置的方法,如果存在,则会覆盖该位置,否则将在继续之前创建目录。

现在,我没有任何清晰的代码(除了我所显示的代码)来显示我的工作,但是如果有人可以将我的工作指向一个很好的例子,(我已经尚未找到有关从Unity中的FTPS下载的更多信息),或者只是分享他们如何从头开始解决此问题,将不胜感激。

作为旁注, 我知道我的FTP服务器正在通过https://ftptest.net/正常运行 我的服务器日志(我自己托管,因此可以完全控制设置)显示所有正确的消息,直到RETR和Transfer Success消息为止。

我已经在FTPS上看到很多帖子,涉及Unity内部和外部的各种下载方式以及所有不同级别的网络通信;高级和低级,以及如何将文件从文件夹实时加载到Unity中。但是,没有任何东西可以一口气将其覆盖,美观又干净。吻!

作为我们大多数人,我有更大的视野要关注,并且不能让这一细节继续拖延开发。

编辑(服务器日志):

这是来自我的WebClient.DownloadAsync();

的服务器日志
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> Connected, sending welcome message...
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> AUTH TLS
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 234 Using authentication type TLS
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> SSL connection established
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PBSZ 0
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 200 PBSZ=0
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PROT P
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 200 Protection level set to P
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> USER Test
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 331 Password required for test
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PASS ********
(000657)08/23/2018 22:51:32 - test ([MyIP])> 230 Logged on
(000657)08/23/2018 22:51:32 - test ([MyIP])> OPTS utf8 on
(000657)08/23/2018 22:51:32 - test ([MyIP])> 200 UTF8 mode enabled
(000657)08/23/2018 22:51:32 - test ([MyIP])> PWD
(000657)08/23/2018 22:51:32 - test ([MyIP])> 257 "/" is current directory.
(000657)08/23/2018 22:51:32 - test ([MyIP])> CWD /
(000657)08/23/2018 22:51:32 - test ([MyIP])> 250 CWD successful. "/" is current directory.
(000657)08/23/2018 22:51:32 - test ([MyIP])> TYPE I
(000657)08/23/2018 22:51:32 - test ([MyIP])> 200 Type set to I
(000657)08/23/2018 22:51:32 - test ([MyIP])> PASV
(000657)08/23/2018 22:51:32 - test ([MyIP])> 227 Entering Passive Mode ([MyIP],195,146)
(000657)08/23/2018 22:51:32 - test ([MyIP])> RETR MainQuests.txt
(000657)08/23/2018 22:51:32 - test ([MyIP])> 150 Connection accepted
(000657)08/23/2018 22:51:32 - test ([MyIP])> SSL connection for data connection established
(000657)08/23/2018 22:51:32 - test ([MyIP])> 226 Transfer OK

值得注意的是,该连接仍处于维护状态。.它不会关闭,因为在文件下载完成后,该连接被设置为关闭...

这是使用C#任务时获得的日志,如Dan Flanagan的示例所示:

(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> Connected, sending welcome message...
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> AUTH TLS
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 234 Using authentication type TLS
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> SSL connection established
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> PBSZ 0
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 200 PBSZ=0
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> PROT P
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 200 Protection level set to P
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> USER Test
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 331 Password required for test
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> PASS ********
(000659)08/23/2018 22:58:10 - test ([MyIP])> 230 Logged on
(000659)08/23/2018 22:58:10 - test ([MyIP])> OPTS utf8 on
(000659)08/23/2018 22:58:10 - test ([MyIP])> 200 UTF8 mode enabled
(000659)08/23/2018 22:58:10 - test ([MyIP])> PWD
(000659)08/23/2018 22:58:10 - test ([MyIP])> 257 "/" is current directory.
(000659)08/23/2018 22:58:10 - test ([MyIP])> CWD /
(000659)08/23/2018 22:58:10 - test ([MyIP])> 250 CWD successful. "/" is current directory.
(000659)08/23/2018 22:58:10 - test ([MyIP])> TYPE I
(000659)08/23/2018 22:58:10 - test ([MyIP])> 200 Type set to I
(000659)08/23/2018 22:58:10 - test ([MyIP])> PASV
(000659)08/23/2018 22:58:10 - test ([MyIP])> 227 Entering Passive Mode ([MyIP],195,147)
(000659)08/23/2018 22:58:10 - test ([MyIP])> RETR MainQuests.txt
(000659)08/23/2018 22:58:10 - test ([MyIP])> 150 Connection accepted
(000659)08/23/2018 22:58:10 - test ([MyIP])> SSL connection for data connection established
(000659)08/23/2018 22:58:10 - test ([MyIP])> 426 Connection closed;    transfer aborted. (000659)08/23/2018 22:58:10 - test ([MyIP])> QUIT
(000659)08/23/2018 22:58:10 - test ([MyIP])> 221 Goodbye
(000659)08/23/2018 22:58:10 - test ([MyIP])> disconnected.

1 个答案:

答案 0 :(得分:1)

我剥离了在Unity中使用过的旧的FTP下载器脚本。您可能想要将文件保存在Streaming Assets文件夹中。这应该可以帮助您。我用它来下载视频,他们从StreamingAssets文件夹中播放了视频,但下载器确实起作用了。

using System;
using System.Threading.Tasks;
using System.IO;
using System.Net;

public class FTPDownloader : MonoBehaviour
{

    public bool editorDownload;
    public string savePath;
    public string saveDir;
    public string username;
    public string pswd;

    public void StartDownload(string serverIP, string serverFolder, string fileName)
    {
        string url = string.Format("{0}{1}{2}{3}", "ftp://", serverIP, serverFolder, fileName);
        Task task = new Task(() =>
        {
            Download(url, fileName);
        });
        task.Start();
    }

    void Download(string url, string fileName)
    {
        if (!editorDownload)
        {
            return;
        }
        string path = Path.Combine(saveDir, fileName);

        try
        {
            if(File.Exists(path))
            {
                //already downloaded
                return;
            }
            //Debug.Log(url);
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
            request.Credentials = new NetworkCredential(username, pswd);

            using (Stream ftpStream = request.GetResponse().GetResponseStream())
            using (Stream fileStream = File.Create(path))
            {
                byte[] buffer = new byte[10240];
                int read;
                int total = 0;
                while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fileStream.Write(buffer, 0, read);
                    total += read;
                }
            }
        }
        catch (Exception e)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            //Debug.Log(e.ToString());
        }
    }
}