c# HttpClient 给webapi post传递并传参

 

复制代码
public  IHttpActionResult SinkingCommunityData([FromBody]CommunityModel Entitys)
{
//自己的业务逻辑代码
//并调用其他接口
string
url = "http://localhost:54150/api/_data"; //创建HttpClient using (var http = new HttpClient(handler)) { http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); StringContent strcontent = new StringContent(JsonConvert.SerializeObject(Entitys), Encoding.UTF8, "application/json"); //异步等待回应 HttpResponseMessage response = http.PostAsync(url, strcontent).Result; //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); string str = response.Content.ReadAsStringAsync().Result; }
}
复制代码

 

复制代码
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent strcontent = new StringContent(JsonConvert.SerializeObject("aa"), Encoding.UTF8, "application/json");
var message = new HttpRequestMessage(HttpMethod.Post, "your address");
//设置cookie信息
message.Headers.Add("Cookie", "token=" + token);
//设置contetn
message.Content = strcontent;
//发送请求
var httpResponseHeaders = httpClient.SendAsync(message).Result;
复制代码

 

posted @   qingjiawen  阅读(1283)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示