日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

httpclient工具类,post请求发送json字符串参数,中文乱码处理

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 httpclient工具类,post请求发送json字符串参数,中文乱码处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在使用httpclient發送post請求的時候,接收端中文亂碼問題解決。

正文:

我們都知道,一般情況下使用post請求是不會出現中文亂碼的。可是在使用httpclient發送post請求報文含中文的時候在發送端數據正常但是到了服務器端就中文亂碼了。

解決辦法:

發送端進行設置編碼如下:

?工具類:

 1 package com.Util;
 2 
 3 import com.google.common.base.Charsets;
 4 import org.apache.http.HttpEntity;
 5 import org.apache.http.client.methods.CloseableHttpResponse;
 6 import org.apache.http.client.methods.HttpPost;
 7 import org.apache.http.entity.StringEntity;
 8 import org.apache.http.impl.client.CloseableHttpClient;
 9 import org.apache.http.impl.client.HttpClients;
10 import org.apache.http.util.EntityUtils;
11 
12 public class HttpUtil {
13     public static String sendHttpPost(String url, String body) throws Exception {
14         CloseableHttpClient httpClient = HttpClients.createDefault();
15         HttpPost httpPost = new HttpPost(url);
16         httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
17         httpPost.setHeader("Accept", "application/json");
18         httpPost.setEntity(new StringEntity(body, Charsets.UTF_8));
19         CloseableHttpResponse response = httpClient.execute(httpPost);
20         System.out.println(response.getStatusLine().getStatusCode() + "\n");
21         HttpEntity entity = response.getEntity();
22         String responseContent = EntityUtils.toString(entity, "UTF-8");
23         response.close();
24         httpClient.close();
25         return responseContent;
26     }
27 
28 }

?

轉載于:https://www.cnblogs.com/newAndHui/p/10818601.html

總結

以上是生活随笔為你收集整理的httpclient工具类,post请求发送json字符串参数,中文乱码处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。