0

I am sending a GCM from my C# server using "application/json" (Downstream HTTP messages (JSON)). Everything works well, my Android devices receive the message.

But the problem is: If I send special characters like ä ë á é or similar then I get this error:

WebException: System.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written.

I tried using:

HttpUtility.UrlEncode("my méssägë hérë")

but then I receive the messages in Android in the wrong format. Any help how to encode the strings correctly?

Vikas Nokhwal
  • 113
  • 10
Ton
  • 9,235
  • 15
  • 59
  • 103

1 Answers1

0

I solved changing:

using (StreamWriter oWriter = new StreamWriter(req.GetRequestStream()))
{  oWriter.Write(msg);
}

with this:

Byte[] byteArray = Encoding.UTF8.GetBytes(msg);
req.ContentLength = byteArray.Length; <-- important
Stream writer = req.GetRequestStream();
writer.Write(byteArray, 0, byteArray.Length);

See also here

Community
  • 1
  • 1
Ton
  • 9,235
  • 15
  • 59
  • 103