Search code examples
c#curlartifactorysystem.diagnostics

Using Curl to download a File from Artifactory in C#


I'm trying to use Curl to download a .zip File from Artifactory. For that I'm using

System.Diagnostics.Process()

Is there a Way to see how far the download progressed or is there a better Way to get a .zip File from Artifactory than my used approach? Here is the Code I'm using, grateful for any suggested improvements

System.Diagnostics.Process process = new System.Diagnostics.Process()
            {
                StartInfo = new System.Diagnostics.ProcessStartInfo()
                {
                    FileName = "curl",
                    Arguments = "-H \"X-JFrog-Art-Api:<Token>\" -X GET \"" + url + "\" -O \"" + path + "\"",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true
                }
            };
            process.Start();

            System.IO.StreamReader reader = process.StandardOutput;
            string output = reader.ReadToEnd();
            process.WaitForExit();

Solution

  • You should use an HttpClient and HttpRequestMessage like this:

    (from https://stackoverflow.com/a/40707446/8800752)

            string baseURL = "";
            string path = "";
            string token = "";
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseURL);
    
                using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, path))
                {
                    requestMessage.Headers.Add("X-JFrog-Art-Api", token);
    
                    var response = client.Send(requestMessage);
                }
            }
    

    You can change it to await + SendAsync and if you're going to make multiple calls, use a HttpFactory or make the client static.