简体   繁体   English

如何在 C# 中使用表单数据构造 HttpClient POST 请求?

[英]How to construct HttpClient POST Request with form-data in C#?

I am having issues constructing the POST request with form-data in C# using HTTPClient.我在使用 HTTPClient 在 C# 中使用表单数据构建 POST 请求时遇到问题。 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.请注意,SWAGGER POST 工作正常,基于此我正在尝试构建请求。

The API parameters API参数

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

The working swagger POST工作招摇的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). SWAGGER POST 的 Fiddler 请求标头(这是有效的)。

提琴手请求详细信息

    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;
    }
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("Value1"),"formparamname1"},
 {new StringContent("Value2"),"formparamname2" },
 {new StringContent("Value2"),"formparamname3"},
};
var response = client.PostAsync(baseUrl, formContent).Result;
response.EnsureSuccessStatusCode();
var result = string.Empty;
if (response.IsSuccessStatusCode)
 {
   result = response.Content.ReadAsStringAsync().Result;
 }
return result;

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

相关问题 在c#HttpClient 4.5中发布multipart / form-data - post multipart/form-data in c# HttpClient 4.5 HttpClient Post REST API 中的 C# 多部分表单数据 - C# Multipart form-data in HttpClient Post REST API C# 如何将带有文件的实体作为表单数据从 HttpClient 发布到 API? - C# How to post an entity with a file as form-data from HttpClient to API? 如何使用 HttpClient 发布表单数据 IFormFile? - How to post form-data IFormFile with HttpClient? 如何从C#提交multipart / form-data HTTP POST请求 - How to submit a multipart/form-data HTTP POST request from C# 使用 C# HttpClient 在没有 multipart/form-data 的情况下发布文件 - Using C# HttpClient to POST File without multipart/form-data 如何使用仅具有键值参数的帮助 HttpClient 发送表单数据 POST 请求 - How to send form-data POST request with the help HttpClient with just Key-Value parameters C# HttpClient 4.5 multipart/form-data 上传 - C# HttpClient 4.5 multipart/form-data upload 使用 HttpClient C# 发送表单数据 - Send form-data using HttpClient C# 使用 .Net 框架 HttpClient class 发送包含文件的表单数据发布请求 - Send an form-data post request with .Net framework HttpClient class containing a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM