Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用编码的非ANSII字符发布Json_C#_Json_Http - Fatal编程技术网

C# 使用编码的非ANSII字符发布Json

C# 使用编码的非ANSII字符发布Json,c#,json,http,C#,Json,Http,我必须用西里尔字母的符号字符串发布Json数据,如下所示: string json = { "message": "Привет" } string json = { "message": "\u041F\u0440\u0438\u0432\u0435\u0442" } 我想我需要发送这样的邮件: string json = { "message": "Привет" } string json = { "message": "\u041F\u0440\u0438\u0432\u0435\

我必须用西里尔字母的符号字符串发布Json数据,如下所示:

string json = { "message": "Привет" }
string json = { "message": "\u041F\u0440\u0438\u0432\u0435\u0442" }
我想我需要发送这样的邮件:

string json = { "message": "Привет" }
string json = { "message": "\u041F\u0440\u0438\u0432\u0435\u0442" }
所以我尝试转义非ascii编码字符:

string privet = "\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442";
但服务器不接受这一点,因为字符串按原样传递,带有两个反斜杠:

string json = { "message": "\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442" }
也不接受UTF-8编码:

byte[] bJson = Encoding.UTF8.GetBytes(json);
如何获取所需格式的字符串


更新

public class Test
{
     public string message { get; set; }
}

Test myTest = new Test{ };

myTest.message = "\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442";
string json = JsonConvert.SerializeObject(myTest, Formatting.Indented);

byte[] sbBites = Encoding.ASCII.GetBytes(json);
Uri url = new Uri("https://example.net");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = sbBites.Length;
request.ContentType = "application/json";

using (Stream requestStream = request.GetRequestStream())
{
     requestStream.Write(sbBites, 0, sbBites.Length);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

难怪您的服务器接收到的值中有一堆反斜杠,因为您发送给它的字符串值中有一堆反斜杠

你试过这个吗:

public class Test
{
     public string message { get; set; }
}

string privet = "Привет";

Test myTest = new Test { };

myTest.message = privet;
string json = JsonConvert.SerializeObject(myTest, Formatting.Indented);

byte[] sbBites = Encoding.UTF8.GetBytes(json);
Uri url = new Uri("https://example.net");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = sbBites.Length;
request.ContentType = "application/json";

using (Stream requestStream = request.GetRequestStream())
{
     requestStream.Write(sbBites, 0, sbBites.Length);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
这是解决办法

string json = JsonConvert.SerializeObject(myTest, Formatting.Indented,
new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii });

很难看出是什么导致请求出现问题,因为问题不包含任何与之相关的代码。。。请查看问题中提供代码的指南。有关自动转义JSON字符串的信息,请参阅。您应该能够将
“ППцццц”literal传递给该方法,并获取正确的值。您是否尝试按原样发布数据,而不干预它?我想应该可以。@JLRishe这是一个很好的建议,但在这种情况下,不幸的是,服务器返回了一个错误。@AntoshaShmonoff错误是什么?它将按原样发送字符串,而服务器只接受转义字符串。还有
Serialize(privet,StringEscapeHandling.escapeEnonascii)
方法,但它返回相同的双反斜杠字符串。。。