• 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");

     

     

  • 相关阅读:
    工程思维
    小骆驼 第三章 列表与数组
    小骆驼 第二章 标量数据
    小骆驼 第二章 标量数据
    小骆驼 第二章 标量数据
    split和join合写
    Competition and Predation
    What is the difference between Θ(n) and O(n)?
    数学基础之概率统计
    enumerate()函数
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/10454927.html
Copyright © 2020-2023  润新知