I have a method, where I use the deprecated WebClient
class to upload a file to a FTP Server
.
public async Task<BaseResponse<Void>> Handle(FTPUploadRequest request, CancellationToken cancellationToken)
{
using WebClient client = new()
{
Credentials = new NetworkCredential(configuration.FTP.User, configuration.FTP.Password.Decrypt())
};
static string BuildUri(FTPUploadRequest request, IOptionsSnapshot<Configuration> configuration)
=> $"ftp://{configuration.FTP.Server}/{request.DirectoryPath}/{request.FileName}";
client.UploadFile(
BuildUri(request, configuration),
WebRequestMethods.Ftp.UploadFile,
request.FilePath + "/" + request.FileName);
return await BaseResponse<Void>.Create(true);
}
WebClient
is obsolete and not Unit-testable
. The message is:
SYSLIB0014 'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.'
So I wanted to inject IHttpClientFactory
which is also stated in the obsolete message. I can easily do that, but how should I upload the file? Posting against a FTP Server
with a multi-part-message
?
I have no idea and cannot find any solution on the internet.
Any Ideas?
You don't.
HTTP and FTP are completely different protocols.
You want to use an FTP Client.
FluentFTP seems like a good bet.