第一步:引用程序集: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);
            }

相关文章:

  • 2021-05-24
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2021-04-26
  • 2021-04-19
  • 2021-11-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-30
  • 2021-12-23
  • 2022-12-23
  • 2021-04-29
  • 2021-10-09
  • 2021-12-12
相关资源
相似解决方案