简体   繁体   English

C#RestSharp RestClient混淆

[英]C# RestSharp RestClient Confusion

I'm setting up an API call that I know works with the given credentials. 我正在设置一个已知调用给定凭据的API调用。 I'm new to C# and .NET in general and I've been told to use RestSharp for API calls. 一般来说,我是C#和.NET的新手,有人告诉我使用RestSharp进行API调用。 What gets passed as an argument to RestClient? 什么作为参数传递给RestClient? I've tried my local server but not sure that's it. 我已经尝试过本地服务器,但是不确定是这样。

Here is the controller code: 这是控制器代码:

    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();
}

The endpoint should be passed into the constructor of RestClient . 端点应传递到RestClient的构造函数中。 The URL segments should be passed into the RestRequst . URL段应传递到RestRequst

From the example on http://restsharp.org/ 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

So in your example, you should declare endpoint as: 因此,在您的示例中,应将端点声明为:

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

and pass hotel-api/1.0/status into the RestRequest constructor. 并将hotel-api/1.0/status传递到RestRequest构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM