zoukankan      html  css  js  c++  java
  • c# 后台异步请求接口

    第一步:引用程序集:Systen.Net.Http

    第一种方式: 异步 Get请求

    HttpClient client = new HttpClient();
                //client.DefaultRequestHeaders.Add("Cookie","xxx");
                string result = await client.GetStringAsync("
    http://localhost:8282/V1/TestNotEncrypt/TestAsync2?name=123&id=1");

    返回值肯定是asnyc Task<类型>

    第二种方式 异步post请求

     string url = "http://localhost:8282/V1/TestNotEncrypt/TestPostResponse";
                HttpClient client = new HttpClient();

              //post传的参数

                HttpContent content = new StringContent(JsonConvert.SerializeObject(new { Id = 1, Name = "张三" }));

        //类型为json
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        
                var httpResponse =await client.PostAsync(url, content);
                if (httpResponse.IsSuccessStatusCode)
                {
                    var result= await httpResponse.Content.ReadAsStringAsync();
                    //序列化result为指定对象
                    return JsonConvert.DeserializeObject<ReturnMsg>(result);
                }

  • 相关阅读:
    bzoj3293 分金币
    考前模板整理
    CF785D Anton and School
    容斥法解决错排问题
    CF1248F Catowice City
    CF1248E Queue in the Train
    CF1244F Chips
    CF1244C The Football Season
    Noip2016Day1T2 天天爱跑步
    Noip2015Day2T3 运输计划
  • 原文地址:https://www.cnblogs.com/LoveAndPeace/p/7484062.html
Copyright © 2011-2022 走看看