带有接受标头.Net Core 3.1的帖子-PagSeguro [英] Post with Accept Header .Net Core 3.1 - PagSeguro
问题描述
根据文档,我正在尝试与支付网关(PagSeguro)集成,我必须使用接受标头" application/vnd.pagseguro.com.br.v3 + json; charset = ISO-8859-1.
I'm trying to integrate with a payment gateway (PagSeguro), according to the documentation, I must use the Accept Header "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1".
尝试使用代码不起作用:
Trying with the code not work:
HttpClient.DefaultRequestHeaders.Clear();
HttpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1");
var content = new StringContent(json, Encoding.Default, "application/json");
var response = await HttpClient.PostAsync(enderecoPreApprovals, content);
var responsestr = await response.Content.ReadAsStringAsync();
我也尝试使用文档中的相同代码:
I also tried using the same code from documentation:
var client = new RestClient(url) {Timeout = -1};
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1");
request.AddParameter("application/json", content, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
并使用flurl:
var response = await addresPreApprovals
.WithHeader("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1")
.WithHeader("Content-Type", "application/json")
.PostJsonAsync(adesaoDto);
所有响应均为 Accept标头字段为必填项.
.就像无法识别接受标头".
All the responses are Accept header field is mandatory.
. It's like Accept Header is not recognized.
API中没有问题,为什么我尝试使用Postman和Insomnia,并且效果很好.
The problem is not in API why i tried using Postman and Insomnia and it's works perfectly.
推荐答案
出现此问题是因为分号(;)和单词字符集之间存在空格.我们无法使其在.NET 3.1中工作,甚至在github上还有一个问题,表明这不是问题,它是RFC 7231规范的实现.每当字符串"var1 = val1;var2 = val2",表示o.NET转换为"var1 = val1;var2 = val2".但是,在.NET 5.0中,发布了一个允许输入原始字符串的实现.根据以前的帖子,另一种选择是使用.NET Framework 4.5创建内容
The problem occurs because of the space between the semicolon (;) and the word charset. We were unable to make it work in .NET 3.1 and there is even an issue on github, indicating that it is not a problem, an implementation of the RFC 7231 specification. Whenever a string "var1 = val1; var2 = val2", o is indicated. NET converts to "var1 = val1; var2 = val2". However, in .NET 5.0, an implementation was released that allows entering a raw string. Another option, according to an old post, is to create something with .NET Framework 4.5
在.NET 5.0中
var acceptValue = "application/vnd.pagseguro.com.br.v1+json;charset=ISO-8859-1";
var requestMessage = new HttpRequestMessage(HttpMethod.Post, urlApprovals);
requestMessage.Headers.TryAddWithoutValidation("accept", acceptValue);
GitHub ref: https://github.com/dotnet/runtime/issues/30171
GitHub ref: https://github.com/dotnet/runtime/issues/30171
使用.NET Framework 4.5解决问题的用户: https://stackoverflow.com/a/40162071/2112736
User who solved the problem using .NET Framework 4.5: https://stackoverflow.com/a/40162071/2112736
这篇关于带有接受标头.Net Core 3.1的帖子-PagSeguro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!