Mollie 发送请求时付款失败https://api.mollie.com/v2/payments
使用HttpClient
类System.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;
}
{
"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 时在邮递员中工作