Interface-oriented programming is a design idea. No matter what language is used, the idea of interface-oriented development is indispensable. In the process of software development, the interface is often called. The next step is to introduce C# to call the interface provided by other developers to obtain data. The http interface method Get interface data.
Get request data:
using (var httpClient = new HttpClient()) { //get var url = new Uri("interface network address"); // response var response = httpClient.GetAsync(url).Result; var data = response.Content.ReadAsStringAsync().Result; return data;//The data obtained by the interface call successfully }
Post request data:
using (var httpClient = new HttpClient()) { //post var url = new Uri("interface network address"); var body = new FormUrlEncodedContent(new Dictionary<string, string> { { "parameter1", "value1"}, { "parameter2", "value2"}, { "parameter 3", "value 3"}, { "parameter 4", "value 4"}, }); // response var response = httpClient.PostAsync(url, body).Result; var data = response.Content.ReadAsStringAsync().Result; return data;//Interface call success data }
If the interface call needs to pass the request header, you can use the following code to set the request header:
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");//Set the request header