• 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;
  • 相关阅读:
    (Go)11.九九乘法表示例
    (Go)10.流程控制示例
    (Go)09.指针赋值修改示例
    (Go)08.time示例
    (Go)07.strings与strconv的示例
    (Go)07.Go语言中strings和strconv包示例代码详解02
    (Go)06. Printf格式化输出、Scanf格式化输入详解
    kafka参数在线修改
    vs code golang代码自动补全
    JVM 方法区
  • 原文地址:https://www.cnblogs.com/qingjiawen/p/16470224.html
Copyright © 2020-2023  润新知