2016-03-15 62 views
0

我使用「application/json」(下游HTTP消息(JSON))從我的C#服務器發送GCM。一切正常,我的Android設備收到消息。GCM發送帶有特殊字符的JSon消息

但問題是:如果我發送特殊字符,如A E A E或類似的話,我得到這個錯誤:

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

我試着使用:

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

後來我收到Android中的消息格式不正確。 任何幫助如何正確地編碼字符串?

回答

0

我解決了改變:

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

與此:

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

參見here