How To download from ftp site using http client

HowardMontemurro-7208 40 Reputation points
2023-05-29T14:03:53.55+00:00

I cannot find a SIMPLE EXAMPLE anywhere that shows how to use (recommended) HTTPCLIENT to replace the now OBSOLETE WebCclient, WebRequest etc.

I cannot find a SIMPLE EXAMPLE anywhere that shows how to use (recommended) HTTPCLIENT to replace the now OBSOLETE WebCclient, WebRequest etc.

I am trying to update an older (2017) Visual Studio project using "windows forms app" template with "Net 6.0 (long term support) and all I get is grief (errors and warnings) for the FTP segments of my coding ( FtpWebRequest, FtpWebResponse, etc.). All the help I get is "USE HTTPLCIENT INSTEAD" which is a nice suggestion if I could find an example thereof to replace the snippets below. I do not need multiple files, progress reports, etc. which seem to obscure the download coding.

Please HELP.

Just a simple example including uri construction, Httpclient, 'GET" logic coding and other stuff (that's a technical term meaning whatever) that I might need and use.

THANK YOU!

  Private ftpQuest As FtpWebRequest 

  '.....

  ftpQuest = CType(WebRequest.Create("ftp://ftps0.mywebsource.com/myload-source/" & 

               myfilename.txt), FtpWebRequest)

  ftpQuest.Credentials = "MyUserName:MyPaassowrd"

  ftpQuest.Method = WebRequestMethods.Ftp.DownloadFile

  Try

    ftpReply = CType(ftpQuest.GetResponse, FtpWebResponse)

  '...

  End try
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,371 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 31,971 Reputation points Microsoft Vendor
    2023-05-30T09:40:57.7166667+00:00

    Hi @HowardMontemurro-7208 , Welcome to Microsoft Q&A.

    For c#, Httpclient has corresponding examples on the official website.

    You can also refer to these two vb cases:HttpWebRequest to HttpClient post Request and VB.net POST method using HTTPclient

    The following vb httpclient code is for reference only:

    Private Async Sub DownloadFileFromFtp()
        Dim ftpUrl As String = "ftp://ftps0.mywebsource.com/myload-source/myfilename.txt"
        Dim userName As String = "MyUserName"
        Dim password As String = "MyPassword"
    
        Using httpClient As New HttpClient()
            Dim credentials As New System.Net.NetworkCredential(userName, password)
            httpClient.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{userName}:{password}")))
            
            Dim response As HttpResponseMessage = Await httpClient.GetAsync(ftpUrl)
    
            If response.IsSuccessStatusCode Then
                Using fileStream As IO.FileStream = Await response.Content.ReadAsStreamAsync()
                    Using outputFileStream As IO.FileStream = IO.File.Create("path\to\save\downloaded\file\myfilename.txt")
                        Await fileStream.CopyToAsync(outputFileStream)
                    End Using
                End Using
    
                ' File downloaded successfully.
                MessageBox.Show("File downloaded successfully.")
            Else
                ' Handle the case when file download fails.
                MessageBox.Show($"File download failed. Status code: {response.StatusCode}")
            End If
        End Using
    End Sub
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.