HttpPost UTF-8 encoding / decoding Java
I am having problems trying to send utf8 formatted post data from client to my server.
I've read a lot about this topic, but I can't seem to find a solution. I'm sure I'm missing something basic.
Here is my client code to make a post request
public static PostResponse post(String url, String data) {
PostResponse response = new PostResponse();
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "text/plain; charset=UTF-8");
// 3. set string to StringEntity
StringEntity se = new StringEntity(data);
// 4. set httpPost Entity
httpPost.setEntity(se);
// 5. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
} catch (IOException e) {
}
return response;
}
This is the code on my server side to get the text in utf8 (Polish characters don't appear, I get "?" Instead.)
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
BufferedReader reader;
reader = req.getReader();
Gson gson = new Gson();
msg = gson.fromJson(reader, MsgChat.class); //String in the client was json
} catch (IOException e) {
}
}
I only wrote the relevant code.
Any help in this regard would be appreciated. Thank.
+3
source to share
3 answers