httpclient get和post,中文乱码已解决

来源:互联网 发布:appstore付费软件 编辑:程序博客网 时间:2024/04/30 08:58

使用httpclient方法

get:

使用get请求,获取url传过来的数据

// 打印响应内容 ,转换为utf-8格式,避免所传内容包含汉字乱码
context = EntityUtils.toString(entity, "UTF-8");
得到的entity,一定要限定编码


实例:

/**
* 发送 get请求
*/
public String get(String url1) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String context="";
try {
URL url = new URL(url1);
URI uri = new URI(url.getProtocol(), url.getHost() + ":" + url.getPort(),url.getPath(), url.getQuery(), null);
// 创建httpget.
HttpGet httpget = new HttpGet(uri);
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容 ,转换为utf-8格式,避免所传内容包含汉字乱码
context = EntityUtils.toString(entity, "UTF-8");
System.out.println(context);
}
response.close();
return context;
} catch (Exception e) {
e.printStackTrace();
return "Exception";
}
finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
post:

post数据给URL,并获得其返回数据

除了接收到的数据要注意编码之外,post给URL的数据也要注意编码问题

tringEntityse=newStringEntity(param.toString(),"UTF-8");//解决传送的Json中文乱码

实例:

/**
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
*/
public String post(String url,JSONObject param) {
String context="";
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
URL url1 = new URL(url);
URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null);
// 创建httppost
HttpPost httppost = new HttpPost(uri);
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString() ,"UTF-8");//解决传送的Json中文乱码
httppost.setEntity(se);
// 发送请求
CloseableHttpResponse response = httpclient.execute(httppost);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
HttpEntity entity = response.getEntity();
if (entity != null) {
context = EntityUtils.toString(entity, "UTF-8");
}
response.close();
return context;
} catch (Exception e) {
e.printStackTrace();
return "Exception";
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 0