REST API 调用 方法

METHOD      DESCRIPTION

GET         Retrieves the specified resource

POST        Creates a resource

PATCH       Updates the specified resource

DELETE      Deletes the specified resource

HEAD        Similar to get except only returns HTTP headers

示例

https://docs.microsoft.com/en-us/IIS-Administration/api/crud

 

代码示例RestSharp

01
02
03
04
05
06
07
08
09
var client = new RestClient(iisAdminBaseUrl);
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;
var request = new RestRequest("/api/webserver/websites", Method.GET);
request.AddHeader("Access-Token", $"Bearer {token}");
request.AddHeader("Accept", "application/hal+json");
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{ …… }

           

代码示例HttpClient

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
var apiClient = new HttpClient(new HttpClientHandler()
{
UseDefaultCredentials = true,
ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true
}, true);
// Set access token for every request
apiClient.DefaultRequestHeaders.Add("Access-Token", "Bearer {token}");
// Request HAL (_links)
apiClient.DefaultRequestHeaders.Add("Accept", "application/hal+json");
 
var res = await apiClient.GetAsync("https://localhost:55539/api/webserver/websites");
if (res.StatusCode != HttpStatusCode.OK) {
  HandleError(res);
  return;
}
JArray sites = JObject.Parse(res.Content.ReadAsStringAsync().Result).Value<JArray>("websites");

 

 

posted @ 2019-03-01 10:23  星火卓越  阅读(2447)  评论(0编辑  收藏  举报