关于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