Restsharp API 身份验证请求

Restsharp API authentication request

我正在尝试使用 RestSharp post 以下 JSON:

{ "username": "test_user", "password": "superkretp4ssword" }

这就是我写的 C# 代码

var client = new RestClient("https://accept.paymobsolutions.com/api/auth/tokens");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", request.AddJsonBody(new { username = "myUsername", password = "myPassword" }), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

但是在调试时 response.content""

响应应该是:

{
  "token": "ZXlKaGlPaUpJVXpVeE1pSX1Y0NJmV5Sn...", // this is your authentication token
  "profile": {
    "id": 28, // this is your merchant_id
    "user": {
      "id": 31,
      "username": "test_user",
      "first_name": "test_user",
      "last_name": "test_user",
    },
    "created_at": "2016-11-20T16:27:20.067296Z",

    ...
  }
}

发送请求失败,因为 "The underlying connection was closed: An unexpected error occurred ..."。检查 ErrorException 字段以查看请求失败的原因。

此异常与 ServicePointManager.SecurityProtocol

有关

对于 .NET 4,使用以下方法解决问题

ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;