-1

I have to POST Json data with cyrillic's symbols strings, like this:

string json = { "message": "Привет" }

I guess that I need to send something like this:

string json = { "message": "\u041F\u0440\u0438\u0432\u0435\u0442" }

So I tried to escape non-ascii encoded characters:

string privet = "\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442";

But the server does not accept this, because string is passing as it is, with double backslashes:

string json = { "message": "\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442" }

UTF-8 encoding not accepted too:

byte[] bJson = Encoding.UTF8.GetBytes(json);

How to get string in required format?


UPDATE

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();
shmnff
  • 647
  • 2
  • 15
  • 31
  • 3
    It is very hard to see what is causing the problem with the request as question does not contain any code related to it... Please check out [MCVE] guidance on providing code in the questions. – Alexei Levenkov Dec 29 '17 at 04:12
  • See [this Q&A](https://stackoverflow.com/q/1242118/335858) for info on escaping JSON strings automatically. You should be able to pass `"Привет"` literal to the method, and get the proper value back. – Sergey Kalinichenko Dec 29 '17 at 04:20
  • Did you try to just post the data as it is, without meddling with it? I imagine that should work. – JLRishe Dec 29 '17 at 05:00
  • @JLRishe that's a well advice, but in this case, unfortunately, the server returns an error. – shmnff Dec 29 '17 at 05:27
  • @AntoshaShmonoff What's the error? – JLRishe Dec 29 '17 at 05:35
  • @JLRishe "wrong data format" with both cases: UTF-8 format and double backslashes – shmnff Dec 29 '17 at 05:43
  • @AntoshaShmonoff What about when you just send the JSON, unmodified? That's what I was asking about. Please show us your actual code. `string json = { "message": "Привет" }` isn't even valid C# code. – JLRishe Dec 29 '17 at 06:01
  • @JLRishe you're right and I'm fully agree with you, but server accept only JS format. How to force `HttpWebRequest` object send stream with only one backslashes in strings? – shmnff Dec 29 '17 at 06:13
  • @AlexeiLevenkov see update – shmnff Dec 29 '17 at 06:50
  • @AntoshaShmonoff So you're telling me that if you change that fourth line to `string privet = "Привет";` instead of that mess of backslashes and character codes you have there, and use UTF8 encoding instead of ASCII, you still get an error? – JLRishe Dec 29 '17 at 07:04

2 Answers2

1

Here is the solution

string json = JsonConvert.SerializeObject(myTest, Formatting.Indented,
new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii });
shmnff
  • 647
  • 2
  • 15
  • 31
0

It's no wonder that your server is receiving the value with a bunch of backslashes in it, because the string value you are sending it has a bunch of backslashes in it.

Have you tried this:

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();
shmnff
  • 647
  • 2
  • 15
  • 31
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • It will send string as it is, while server accept only escaped one. – shmnff Dec 29 '17 at 09:17
  • there is also `Serialize(privet, StringEscapeHandling.EscapeNonAscii)` method, but it returns the same double backslashes string... – shmnff Dec 29 '17 at 09:42