使用 HttpClient 發送 post 請求,但發出去的請求體里的英文正常,中文全是問號,需要設置以下
HttpClient client = HttpClients.createDefault();
// 構造 POST 參數
ArrayList<NameValuePair> postData = new ArrayList<>();
postData.add(new BasicNameValuePair("username", "name"));
postData.add(new BasicNameValuePair("pwd", "pwd"));
HttpPost post = new HttpPost("http://xxx.xx.com");
// 首先Header部分需要設定字符集為:uft-8
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 此處也需要設定
post.setEntity(new UrlEncodedFormEntity(postData, "utf-8"));
post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Encoding", "gzip, deflate");
post.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
post.setHeader("Connection", "keep-alive");
post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
// 執行請求
HttpResponse response = client.execute(post);
這樣,發出去的中文參數便正常了