簡體   English   中英

C#RestSharp RestClient混淆

[英]C# RestSharp RestClient Confusion

我正在設置一個已知調用給定憑據的API調用。 一般來說,我是C#和.NET的新手,有人告訴我使用RestSharp進行API調用。 什么作為參數傳遞給RestClient? 我已經嘗試過本地服務器,但是不確定是這樣。

這是控制器代碼:

    const string endpoint = "https://api.test.hotelbeds.com/hotel-api/1.0/status";

    string signature;

    using (var sha = SHA256.Create())
    {
        long ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;
        Console.WriteLine("Timestamp: " + ts);
        var computedHash = sha.ComputeHash(Encoding.UTF8.GetBytes(apiKey + sharedSecret + ts));
        signature = BitConverter.ToString(computedHash).Replace("-", "");
    }

    var client = new RestClient("?");
    var request = new RestRequest(endpoint, Method.GET);

    request.AddHeader("X-Signature", signature);
    request.AddHeader("Api-Key", apiKey);            

    var  response = client.Execute(request);
    Debug.WriteLine("this is the response" + response);

    return View();
}

端點應傳遞到RestClient的構造函數中。 URL段應傳遞到RestRequst

http://restsharp.org/上的示例中

var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource

因此,在您的示例中,應將端點聲明為:

https://api.test.hotelbeds.com/

並將hotel-api/1.0/status傳遞到RestRequest構造函數中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM