.NET Core 中的 FTP 客户端 [英] FTP client in .NET Core

查看:75
本文介绍了.NET Core 中的 FTP 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 netcoreapp1.0 通过 FTP 协议下载文件/列表文件吗?

Can I download file / list files via FTP protocol using netcoreapp1.0?

我知道,我可以使用 FtpWebRequest或者 FluentFTP 如果我的目标是完整的 .net45 框架.

I know, I can use FtpWebRequest or FluentFTP if I target full .net45 framework.

然而,我的解决方案完全基于 .NET Standard 1.6,我不想仅仅为了拥有 FTP 就支持完整的框架.

My solution, however, is all based on .NET Standard 1.6 and I don't want to support full framework just to have FTP.

推荐答案

FtpWebRequest 现在在 .NET Core 2.0 中得到支持.请参阅 GitHub 存储库

FtpWebRequest is now supported in .NET Core 2.0. See GitHub repo

示例用法:

public static byte[] MakeRequest(
    string method, 
    string uri, 
    string username, 
    string password, 
    byte[] requestBody = null)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Credentials = new NetworkCredential(username, password);
    request.Method = method;
    //Other request settings (e.g. UsePassive, EnableSsl, Timeout set here)

    if (requestBody != null)
    {
        using (MemoryStream requestMemStream = new MemoryStream(requestBody))
        using (Stream requestStream = request.GetRequestStream())
        {
            requestMemStream.CopyTo(requestStream);
        }
    }

    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    using (MemoryStream responseBody = new MemoryStream())
    {
        response.GetResponseStream().CopyTo(responseBody);
        return responseBody.ToArray();
    }
}

其中 method 参数的值设置为 System.Net.WebRequestMethods.Ftp 的成员.

Where the value for the method parameter is set as a member of System.Net.WebRequestMethods.Ftp.

另见 FTP 示例

这篇关于.NET Core 中的 FTP 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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