FTP client in .NET Core

前端 未结 5 1050
不知归路
不知归路 2021-01-08 01:09

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

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

5条回答
  •  礼貌的吻别
    2021-01-08 01:40

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

    Example usage:

    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();
        }
    }
    

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

    See also FTP Examples

提交回复
热议问题