關於C# 使用HttpContent調用接口


1.c# 關於Content與ContentType之前的對應關系:

   MultipartFormDataContent=》multipart/form-data

   FormUrlEncodedContent=》application/x-www-form-urlencoded

   StringContent=》application/json等

   StreamContent=》binary

2.c#中request和response的編碼和請求類型設置

                // dict為Dictionary<string, string>,from類型數據
                HttpContent httpContent = new FormUrlEncodedContent(dict);
 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); httpContent.Headers.ContentType.CharSet = "GBK"; var response = _HttpClient.PostAsync(url, httpContent).Result; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); response.Content.Headers.ContentType.CharSet = "GBK";
// 接口返回數據
responseJson = response.Content.ReadAsStringAsync().Result;

 

參考:https://www.cnblogs.com/jerryqi/p/11592676.html

   


免責聲明!

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



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