使用http调用接口的办法
//下载using System.Net.Http;
项目中的具体使用的方法
get
|
1
2
|
HttpClient client = new HttpClient();client.GetStringAsync("你定义的要访问的ip").Result; |
post
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
HttpClient client = new HttpClient();<br> //设置接收参数类型 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(); //根据接口数据的写的类型,获取有用的数据 |
过去的永远过去,未来的一直在等待.
转自: https://www.cnblogs.com/hkyyqqq/p/7486087.html