小编Mbm*_*Mbm的帖子

编码/解码HttpPost UTF-8 Java

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

我已经阅读了很多关于这个主题但我找不到解决方案.我确信我遗漏了一些基本的东西.

这是我发布帖子请求的客户端代码

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) {

        } …
Run Code Online (Sandbox Code Playgroud)

java servlets httpclient utf-8

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

httpclient ×1

java ×1

servlets ×1

utf-8 ×1