C# RestSharp RestClient Confusion
本文关键字:Confusion RestClient RestSharp | 更新日期: 2023-09-27 18:35:14
我正在设置一个我知道可以使用给定凭据的 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
构造函数。