在尝试使用PostAsJsonAsync时,我遇到了奇怪的问题。在我的身体里,我得到了这个有着奇怪的主角和尾随角色的json:
99 {
"SessionReferenceId":"f39dc178-279e-4e3a-bda9-a16829eb0e45",
GameReferenceId:“netent_es_starburst”,
"CurrencyCode":"EUR“
"LossLimit":100,
"ClientType":1
}
0
在API方面,这不能绑定,我收到错误消息,即请求不能有空体。
发送端的代码如下所示:
using(var client = new SGSClient()) {
var model = new CashSessionCreateModel()
{
ClientType = ClientType.DesktopBrowser,
CurrencyCode = "EUR",
GameReferenceId = "netent_es_starburst",
LossLimit = 100,
SessionReferenceId = "f39dc178-279e-4e3a-bda9-a16829eb0e45"
};
HttpResponseMessage response = client.PostAsJsonAsync(apiUrl, model).Result;
}添加HTTPClient配置:
public SGSHttpClient()
{
var appSettingsFilePath = $"Configuration\\appSettings.json";
// Build Configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(appSettingsFilePath, false, true)
.AddEnvironmentVariables()
.Build();
var sgsConfig = configuration.GetSection("SGSClient");
//Base url for SGS service
var _clientConfig = sgsConfig.GetSection("Client").GetChildren();
var baseAddress = _clientConfig.FirstOrDefault(o => o.Key.Equals("BaseAddress"));
BaseAddress = new Uri(baseAddress.Value);
//Adding headers
DefaultRequestHeaders.Accept.Clear();
DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_dateUTC = DateTime.UtcNow.ToString("u");
DefaultRequestHeaders.Add("DateUtc", _dateUTC);
}发布于 2022-01-31 19:00:40
这和one other SO question是我今天在这个问题上绞尽脑汁时发现的唯一有用的页面。然而,这两个问题都没有令人满意的解释。因此,经过更多的挠头之后,我可以放心了。希望my answer在另一个问题上解释你的行为,让你满意!
tl;dr
PostAsJsonAsync使用JsonContent,它使用Transfer-Encoding: chunked而不是Content-Length,“奇怪的字符”是块头(并且完全有效的HTTP1.1)
发布于 2019-11-15 09:07:26
您可以使用此类型格式发送可能有帮助。
var postTask = client.PostAsJsonAsync<ClassNameOfObject>(baseUri + "ControllerName/ActionName?UserID=" + UserID, object);
postTask.Wait();https://stackoverflow.com/questions/58745025
复制相似问题