简体   繁体   English

对C#HttpClient的等效curl请求

[英]Equivalent curl request to c# HttpClient

How to make following curl request in c# HttepClient 如何在C#HttepClient中进行以下curl请求

curl -X POST “ https://api.knurld.io/v1/endpointAnalysis/file ” \\ -H "Authorization: $AUTHORIZATION" \\ -H "Developer-Id: $DEVELOPER_ID" \\ -H “multipart/form-data” \\ -F "filename=PATH_TO_FILE" curl -X POST“ https://api.knurld.io/v1/endpointAnalysis/file ” \\ -H“授权:$ AUTHORIZATION” \\ -H“开发人员ID:$ DEVELOPER_ID” \\ -H“ multipart / form-data ” \\ -F“ filename = PATH_TO_FILE”

This Is the sync implemntation but I hope is what you need 这是同步信号,但我希望这是您需要的

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

Kind Regards 亲切的问候

For HttpClient: 对于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();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM