How to send special characters in HttpPost payload c#

问题描述 投票:0回答:0

我正在使用

post
类从客户端调用我的 .net 核心 api,我的 api 托管在 Azure 上。 当有效负载传递给带有简单数据且没有任何特殊字符的 api comntroler 方法时,调用会成功执行,但是当使用“&`` 等特殊字符进行调用时,它会被 api 网关拒绝,状态代码为
httpclient
。我们无法更改 Azure api 网关政策,所以我需要如何更改我的帖子数据内容以在帖子方法中发送特殊字符。

客户电话样本 forbidden

HTPP 内容创作者

using System; class Program { static void Main(string[] args) { Task.Run(() => MainAsync()); Console.ReadLine(); } static async Task MainAsync() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://postCallPoc.net"); var emp = new Employee() { Name = "Jhon", Address = "House no 20, near Clutch & Gair showroom Mumbai", DOJ = DateTime.Now.Date, Note = "Verificaion Pending" }; var httpContent = GetSerializeContent(emp); var result = await client.PostAsync("/api/Employee/add", httpContent); Console.WriteLine(result.StatusCode); //receiving the responce as Forbidden } } public class Employee { public int? ID { get; set; } public string Name { get; set; } public string Address { get; set; } public DateTime DOJ { get; set; } public string Note { get; set; } } }

API 控制器

private ByteArrayContent GetSerializeContent(dynamic content) { var serilizedContent = JsonConvert.SerializeObject(content); var buffer = System.Text.Encoding.UTF8.GetBytes(serilizedContent); var result = new ByteArrayContent(buffer); result.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return result; }


c# json azure asp.net-core-webapi
© www.soinside.com 2019 - 2024. All rights reserved.