C# HttpClient Post 参数同时上传文件

Demo 如下

using (var client = new HttpClient())
{
    using (var multipartFormDataContent = new MultipartFormDataContent())
    {
        var values = new[]
        {
            new KeyValuePair("c", "3"),
            new KeyValuePair("c", "2"),
            new KeyValuePair("d", "2")
             //other values
        };

        foreach (var keyValuePair in values)
        {
            multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
                String.Format("\"{0}\"", keyValuePair.Key));
        }

        multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:\test.jpg")),
            "\"pic\"",
            "\"test.jpg\"");

        var requestUri = "http://localhost:8080";
        var html = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
    }
}

 

你可能感兴趣的:(SQL代码片段)