C# RESTful WebApi 以 json 格式以阿拉伯语发布数据,但正在接收



在C# WebApi中,在尝试以json格式接收阿拉伯语数据(通过Soap UI发送(时,我收到"???"而不是实际的阿拉伯语文本。

网址:

http://localhost:4321/receive/message

JSON 格式(请求(:

{
   "message_no";"123",
   "user_id":"a123",
   "text":"أهلا بك",
}

型:

public class MessageBody
{
        [JsonProperty(PropertyName = "message_no")]
        public string MessageNo { get; set; }
        [JsonProperty(PropertyName = "user_id")]
        public int UserId { get; set; }
        [JsonProperty(PropertyName = "text")]
        public int Text { get; set; }
}

收到的内容:

留言编号: 123

用户标识: a123

发短信:??????

我认为您在服务器端拥有从 UTF-8 混合的字节,任何客户端代码也可以使用各种字符串编码格式来显示测试。

对于客户端:

仅用于使用 HTML 页面发布数据。

除了UTF-8格式保存文件。

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />

对于服务器端:

将下面添加到web.config中,并将其放入<system.web>元素中。

<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8"/>
<</div> div class="one_answers">

将字符串传递给此方法。这将返回所需的文本。

public string DecodeEncodedNonAsciiCharacters(string value)
            {
                return Regex.Replace(
                    value,
                    @"\u(?<Value>[a-zA-Z0-9]{4})",
                    m =>
                    {
                        return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
                    });
            }

您将文本声明为 int。它应该是字符串

[JsonProperty(PropertyName = "text"(]public int Text { get; set; }

最新更新