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

   

posted @ 2022-03-26 22:41  蜡笔小平头  阅读(4457)  评论(0)    收藏  举报