C# HttpPost 【ContentType:multipart/form-data】表單提交數據方法


var postContent = new MultipartFormDataContent();
postContent.Headers.Add("ContentType", $"multipart/form-data");

//這是【string : string】的鍵值對
postContent.Add(new StringContent(appConfig.apiName), "api_name");

//這是【string : Json】的鍵值對;param是一個類的對象
postContent.Add(new StringContent(param.ToJsonStr()), "content");


//form表單格式傳參

string result = "";

string url = "http://xxxxx.cn";
try
{
  HttpClient client = new HttpClient();
  HttpResponseMessage response = client.PostAsync(url, postData).Result;
  result = response.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
  result = ex.Message;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM