在C#中使用Curl从Artifactory下载文件

3qpi33ja  于 2022-11-13  发布在  C#
关注(0)|答案(1)|浏览(190)

我尝试使用Curl从Artifactory下载一个.zip文件。为此我使用

System.Diagnostics.Process()

有没有一种方法可以查看下载进度,或者有没有比我使用的方法更好的方法从Artifactory获取.zip文件?以下是我正在使用的代码,感谢任何改进建议

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

cu6pst1q1#

您应该像这样使用HttpClient和HttpRequestMessage:
(from(第10页)

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

您可以将其更改为await + SendAsync,如果您要进行多个调用,请使用HttpFactory或使客户端成为静态的。

相关问题