关于c#里面的httpclient的调用
使用http调用接口的办法
//下载using System.Net.Http;
项目中的具体使用的方法
get
HttpClient client = new HttpClient(); client.GetStringAsync("你定义的要访问的ip").Result;
post
HttpClient client = new HttpClient();
//设置接收参数类型 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //路径 string url = ""; //数据 var json = "{ \"Name\": \"Test\" }"; //数据进行封装到httpcontent里面去 var httpContent = new StringContent(json, Encoding.UTF8); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); //需要读取数据的时候加await string apiResult = await httpClient.PostAsync(uri, httpContent).Result.Content.ReadAsStringAsync(); //根据接口数据的写的类型,获取有用的数据
过去的永远过去,未来的一直在等待.