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);
}