【问题标题】:Restsharp API authentication requestRestsharp API 认证请求
【发布时间】:2017-07-30 16:20:07
【问题描述】:

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

{ "username": "test_user", "password": "super$3kretp4ssword" }

这是我写的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",

    ...
  }
}

【问题讨论】:

  • response 变量的其他字段是什么?
  • @Progman 请查看编辑
  • 不,您的response 变量的其他字段是什么?特别是 StatusCode 字段。
  • response.StatusCode = 0
  • response 变量中的所有字段是什么?请编辑问题以包含所有字段以及它们从 response 变量中获得的值。它们可能包含请求失败的原因信息。在您的情况下,它看起来甚至没有发送到服务器。

标签: c# restsharp


【解决方案1】:

此异常与ServicePointManager.SecurityProtocol有关

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

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

【讨论】:

    【解决方案2】:

    发送请求失败,因为“底层连接已关闭:发生意外错误...”。检查ErrorException 字段以查看您的请求失败的原因。

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 2012-08-19
      相关资源
      最近更新 更多