java HttpClient 4.3.6的jar包
为什么我get post 请求中文乱码
还有怎么请求的时候怎么带参数,我这样写有什么问题没有,现在的情况就是乱码
public static void get() {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://api.a.7xz.com/v140/ceshi");
httpget.addHeader(new BasicHeader("npi", "3"));
// httpget.addHeader("", "");
CloseableHttpResponse httpReponse = httpClient.execute(httpget);
try {
System.out.println(httpReponse.getStatusLine());
HttpEntity entity = httpReponse.getEntity();
System.out.println(EntityUtils.toString(entity));
} finally {
httpReponse.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void post() {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://api.a.7xz.com/v140/ceshi");
List<BasicNameValuePair> Listnvps = new ArrayList();
Listnvps.add(new BasicNameValuePair("npi", "3"));
httpPost.setEntity(new UrlEncodedFormEntity(Listnvps, "UTF-8"));
CloseableHttpResponse httppHttpResponse2 = httpClient
.execute(httpPost);
try {
System.out.println(httppHttpResponse2.getStatusLine());
System.out.println(EntityUtils.toString(httppHttpResponse2
.getEntity()));
} finally {
httppHttpResponse2.close();
}
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String strResult = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
或者把UTF-8改成相应的编码。
带参数就是拼接在url后面,http://api.a.7xz.com/v140/ceshi?a=11&b=22
乱码,估计还是编码没对应
导入HttpClient 4.3.6的jar包,用你的代码测试了。
你说的是{"yxxfGameName":"","yxxfId":"","yxxfTime":"","yxxfFname":"","yxxfGameSize":"","gid":"","yxxfDownLoadUrl":"","xyceXz":"","yxxfPicUrl":"","flagText":"\u4eca\u65e5\u6d4b\u8bd5","flag":1}中\u4eca\u65e5\u6d4b\u8bd5这种乱码吗?这不是乱码,这是编码了而已。或者你截图看看什么样的乱码。
根据httpReponse 的编码解码得到的内容。