When sending post, set utf-8, Chinese or distortion?
We use HttpUrlConnection or send a post request HttpClient, including Chinese, although we are set up on both sides utf-8, but still garbled?
In the end we request and response should be set up as follows:
request.setCharacterEncoding("utf-8");
post.setHeader("Content-type", "application/json; charset=utf-8");
But for the Chinese, we have to be url encoded and decoded
URLEncoder.encode(param); //url编码
param= URLDecoder.decode(param); //url解码
Although both methods obsolete, but jdk8 and did not remove it, we can still use. This is a simple method to take, if the above method does not work, we'll add codec, but throws UnsupportedEncodingException
an exception, to deal with.
URLEncoder.encode(param,"utf-8"); //url编码
param= URLDecoder.decode(param,"utf-8"); //url解码