c# 后台异步请求接口

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

第一种方式: 异步 Get请求app

 

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

 

第二种方式 异步post请求url

 

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

          //post传的参数对象

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

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

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

相关文章
相关标签/搜索