Httpurlconnection urlconn = (httpurlconnection) URL. openconnection ();
Urlconn. Connect ();
Dataoutputstream out = new dataoutputstream (urlconn. getoutputstream ());
String content = "questions about passing Chinese characters ";
Out. writebytes (content );
Out. Flush ();
Out. Close ();
Urlconn. Disconnect ();
According to the above method, the server must get a bunch of garbled characters, because: Out. writebytes (content); this statement has become garbled during conversion to Chinese.
Public final void writebytes (string s) throws ioexception {
Int Len = S. Length ();
For (INT I = 0; I <Len; I ++ ){
Out. Write (byte) S. charat (I ));
}
Inccount (LEN );
}
Because the char type in Java is 16-bit, a char can store a Chinese character. After converting it to a byte, the 8-bit high will be lost, in this way, Chinese characters cannot be completely output to the output stream. Therefore, it is best to convert the output of a Chinese character into a byte array first, and then write the data into the stream through write. Currently, this method has been tried: put out in the above link code. writebytes (content); Replace with out. write (content. getbytes (); first, convert the data into bytes in the write stream. The execution is successful and the server receives the correct Chinese content.