zoukankan      html  css  js  c++  java
  • 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");

     

     

  • 相关阅读:
    C语言实现链表
    获取两个数之间的随机数-java
    C#继承机制 多级继承
    C#继承机制 访问与隐藏基类成员
    C#继承机制 C#中的继承符合下列规则
    C#装箱与拆箱的研究
    C#箴言之用属性来访问类的私有成员
    C# 创建和初始化集合对象
    C# 常用函数和方法集汇总
    C# 多态与new关键字
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/10454927.html
Copyright © 2011-2022 走看看