编码/解码 HttpPost UTF-8 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25125781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:24:42  来源:igfitidea点击:

Encode/Decode HttpPost UTF-8 Java

javaservletsutf-8httpclient

提问by Mbm

I am having troubles trying to send post data in utf8 format from the client to my server.

我在尝试将 utf8 格式的发布数据从客户端发送到我的服务器时遇到了麻烦。

I have read a lot about this topic but I can′t find the solution. I am sure that I am missing something basic.

我已经阅读了很多关于这个主题的内容,但我找不到解决方案。我确定我错过了一些基本的东西。

Here is my client code to make the 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 in my server side to receive the text in utf8 (Polish characters are not appearing, I am getting "?" instead.)

这是在我的服务器端接收 utf8 文本的代码(波兰语字符没有出现,我得到的是“?”。)

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 wrote only the relevant code.

我只写了相关的代码。

I would appreciate any help regarding this. Thanks.

我将不胜感激。谢谢。

回答by Viet Tran

I had the same problem. You should use this code:

我有同样的问题。您应该使用以下代码:

httpPost.setEntity(new StringEntity(json, "UTF-8"));

Hope this can help you.

希望这可以帮到你。

回答by kan

As I see you should use new StringEntity(data, ContentType.APPLICATION_JSON). By default it is iso-8859-1, setHeaderis not correct way to set encoding as I understand it.

正如我所见,您应该使用new StringEntity(data, ContentType.APPLICATION_JSON). 默认情况下iso-8859-1setHeader根据我的理解,它不是设置编码的正确方法。

回答by Debasys

I'd suggest you try the addRequestHeader method of the HttpPost instead like

我建议您尝试使用 HttpPost 的 addRequestHeader 方法,而不是

httpPost.addRequestHeader("Content-Type", "text/plain;charset=UTF-8");

as that is the recommended way of adding content type header to your request.

因为这是向您的请求添加内容类型标头的推荐方式。