天天看点

C#(.net core)调用webapi

//get方式
				var id= "123456789";
                string result = "";
                using (HttpClient httpClient = new HttpClient())
                {
                    HttpResponseMessage response = httpClient.GetAsync("带http接口地址/" + "方法名/" + "?Id=" + id).Result;
                    //Http响应成功
                    if (response.IsSuccessStatusCode)
                    {
                    	//获取webapi接口数据
                        result = response.Content.ReadAsStringAsync().Result;
                    }
                }
                if (result != "")
                {
                    //将json转成实体
                    var tempList = JsonConvert.DeserializeObject<TemplateData>(result);
                }
             //post方式
             //参数
                var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
                HttpResponseMessage response = httpClient.PostAsync("带http接口地址/" + "方法名/", content).Result;
                //确保Http响应成功
                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }