0

Mollie 发送请求时付款失败https://api.mollie.com/v2/payments

使用HttpClientSystem.Net.Http

使用 API 的 Mollie 付款请求v2/payments工作正常,Postman但在代码unauthorized中出现请求错误c#

下面的代码

HttpClient _httpClient = new HttpClient();

public HttpRequestMessage CreateHttpRequest(HttpMethod method, string relativeUri, HttpContent content = null)
{
    HttpRequestMessage httpRequest = new HttpRequestMessage(method, new Uri(new Uri(ApiEndPoint), relativeUri));
    httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this._apiKey);
    httpRequest.Content = content;

    return httpRequest;
}

public HttpResponseMessage SendHttpRequestAsync(HttpMethod httpMethod, string relativeUri, object data = null)
{
    HttpRequestMessage httpRequest = this.CreateHttpRequest(httpMethod, relativeUri);
    if (data != null)
    {       
        var content = new StringContent(new JavaScriptSerializer().Serialize(data), Encoding.UTF8, "application/json");
        httpRequest.Content = content;
    }

    HttpResponseMessage response = _httpClient.GetAsync(httpRequest.RequestUri.AbsoluteUri).Result;
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(response);
    if (response.IsSuccessStatusCode)
    {
        //here main logic
    }

    return response;
}

Mollie 服务器收到的消息 莫莉留言

{
  "Version": {
    "Major": 1,
    "Minor": 1,
    "Build": -1,
    "Revision": -1,
    "MajorRevision": -1,
    "MinorRevision": -1
  },
  "Content": {
    "Headers": [
      {
        "Key": "Content-Length",
        "Value": [
          "209"
        ]
      },
      {
        "Key": "Content-Type",
        "Value": [
          "application/hal+json"
        ]
      }
    ]
  },
  "StatusCode": 401,
  "ReasonPhrase": "Unauthorized Request",
  "Headers": [
    {
      "Key": "X-Content-Type-Options",
      "Value": [
        "nosniff"
      ]
    },
    {
      "Key": "Strict-Transport-Security",
      "Value": [
        "max-age=31536000; includeSubDomains; preload"
      ]
    },
    {
      "Key": "Date",
      "Value": [
        "Tue, 21 Jan 2020 06:11:41 GMT"
      ]
    },
    {
      "Key": "Server",
      "Value": [
        "nginx"
      ]
    },
    {
      "Key": "WWW-Authenticate",
      "Value": [
        "Basic realm=\"Mollie API Key\""
      ]
    }
  ],
  "RequestMessage": {
    "Version": {
      "Major": 1,
      "Minor": 1,
      "Build": -1,
      "Revision": -1,
      "MajorRevision": -1,
      "MinorRevision": -1
    },
    "Content": null,
    "Method": {
      "Method": "GET"
    },
    "RequestUri": "https://api.mollie.com/v2/payments",
    "Headers": [],
    "Properties": {}
  },
  "IsSuccessStatusCode": false
}

但主要问题是 Test_api 在点击 URL 时在邮递员中工作

邮递员形象

4

3 回答 3

0

试试这种方式它会起作用:

using(var httpClient = new HttpClient()) {
 httpClient.SetBearerToken(tokenValue);
 var serData = JsonConvert.SerializeObject(modelData);
 var httpRequestContentData = new StringContent(serData, Encoding.UTF8, "application/json");
 using(var result = httpClient.PostAsync(url, httpRequestContentData).Result) {
  if ((int) result.StatusCode == (int) HttpStatusCode.OK) {
   // Go Ahead 
  }

 }
}
于 2020-01-21T08:25:05.013 回答
0

关于您的情况,您应该Bearer在标题上添加_httpclient

_httpClient.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue("Bearer", this._apiKey);

现在,由于您将请求正文发送到GET属于 HTTP 规范灰色区域的有点非常规构造的请求 - 最终结果是许多旧软件要么根本无法处理这样的请求,要么将明确拒绝它,因为他们认为它是畸形的。

.NET Framework 不支持这种开箱即用(您将收到一个 .NET Framework ProtocolViolationException)。您可以在构建客户端时安装System.Net.Http.WinHttpHandler和使用它而不是默认值:HttpClientHandler

var handler = new WinHttpHandler();
HttpClient _httpClient = new HttpClient(handler);
//Same code as in question
于 2020-01-21T09:22:40.417 回答
-1

调用会在内部_httpClient.GetAsync(Uri)创建一个全新的HttpRequestMessage对象,而不是使用配置了Authorization:标头的对象。

_httpClient.SendAsync(httpRequest)改为使用实际使用您的对象。

于 2020-01-21T07:12:29.370 回答