如何在C#中构造带有表单数据的HttpClient POST请求?

4

I am having issues constructing the POST request with form-data in C# using HTTPClient. i tried multiple approaches but nothing seems to work. Can you please help what I am missing?

Please be informed, the SWAGGER POST is working fine and based on that I am trying to construct the request.

The API parameters

rPath* string
(formData)
nName* string
(formData)
nFile string
(formData)
type* string
(formData)  
zFile file
file

The working swagger POST

curl -X POST "http://localhost:8888/server/api/v1.0/createfolder" -H "accept: text/plain; charset=UTF-8" -H "authorization: Basic XXXXXXX" -H "Content-Type: multipart/form-data" -F "rPath=/RootFolder/" -F "nName=testing" -F "type=Folder"

The Fiddler request header of the SWAGGER POST (This is working).

Fiddler Request details

    static void Main(string[] args)
    {
        var formData = new MultipartFormDataContent();
        HttpContent content = new FormUrlEncodedContent (new[]
        {
            new KeyValuePair<string, string>("rPath",  "/RootFolder/"),
            new KeyValuePair<string, string>("nName", "TestFolder"),
            new KeyValuePair<string, string>("type", "Folder")
        });           
        content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
        content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
         formData.Add(content);
        var url = "http://localhost:8888/server/api/v1.0/createfolder";
        HttpResponseMessage response = PostRoot(formData, url);
    }
    static HttpResponseMessage PostRoot(HttpContent content, string webMethod)
    {
        HttpResponseMessage response = new HttpResponseMessage();
        using (var client = new HttpClient() {  })
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "xxxxxxx=");
            response = client.PostAsync(webMethod, content).Result;
        }

        return response;
    }

1个回答

4

以下是发送多部分表单数据的示例:

var client = new HttpClient();
var baseUrl = "https://someurl";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");

var formContent = new MultipartFormDataContent
{
    {new StringContent("ParamValue1"),"ParamName1"},
    {new StringContent("ParamValue2"),"ParamName2"},
    {new StringContent("ParamValue2"),"ParamName3"},
};

var response = client.PostAsync(baseUrl, formContent).Result;
response.EnsureSuccessStatusCode();

var result = string.Empty;
if (response.IsSuccessStatusCode)
{
    result = response.Content.ReadAsStringAsync().Result;
}
return result;

2
请不要仅仅发布代码作为答案,还要提供代码的解释以及它是如何解决问题的。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Ran Marciano
"ParamName" 变量可以用引号写成:""ParamName1""。 - S.Serpooshan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接