相当于c# HttpClient的curl请求

本文关键字:curl 请求 HttpClient 相当于 | 更新日期: 2023-09-27 18:12:57

如何在c# httpclient中创建以下curl请求

curl -X POST " https://api.knurld。io/v1/endpointAnalysis '/文件"-H "Authorization: $ Authorization " '-H "Developer-Id: $DEVELOPER_ID" '-H " multipart/form-data-F "filename=PATH_TO_FILE"

相当于c# HttpClient的curl请求

这是同步实现,但我希望是你需要的

    public void Send(string auth, string filePath, string developerId)
    {
        string payload = System.IO.File.ReadAllText(filePath);
        var content = new StringContent(payload);
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", auth);
            client.DefaultRequestHeaders.Add("Content-Type", "multipart/form-data");
            client.DefaultRequestHeaders.Add("Developer-Id", developerId);
            var result = client.PostAsync("https://api.knurld.io/v1/endpointAnalysis/file", content).Result;
            string resultContent = result.Content.ReadAsStringAsync().Result;
        }
    }

亲切的问候

For HttpClient:

async Task Send(string developerId, string pathToFile, string auth)
    {
        using (HttpClient c = new HttpClient())
        {
            c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(auth);
            c.DefaultRequestHeaders.Add("Developer-Id", developerId);
            var multipartFormDataContent = new MultipartFormDataContent();
            using (Stream fileStream = new FileStream(pathToFile, FileMode.Open))
            {
                multipartFormDataContent.Add(new StreamContent(fileStream));
                HttpResponseMessage httpResponse = await c.PostAsync(@"https://api.knurld.io/v1/endpointAnalysis/file", multipartFormDataContent);
                string response = await httpResponse.Content.ReadAsStringAsync();
            }
        }
    }