使用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(); //根據接口數據的寫的類型,獲取有用的數據