public static String sendPut(String url ,Map<String ,Object> map ){
//谷歌的Gson
Gson gson = new Gson();
//相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
HttpClient httpClient = HttpClients.createDefault();
//声明请求方式
HttpPut httpPut = new HttpPut(url);
//设置消息头
httpPut.setHeader("Content-Type","application/json;charset=utf-8");
httpPut.setHeader("Accept","application/json");
//设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
httpPut.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = null;
String result = "";
try {
httpResponse = httpClient.execute(httpPut);
//拿到实体
HttpEntity httpEntity= httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity,"utf-8");
}
//关闭连接
httpPut.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return result ;
}
public static String sendPost(String url ,Map<String ,Object> map) {
//谷歌的Gson
Gson gson = new Gson();
//相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
HttpClient httpClient = HttpClients.createDefault();
//声明请求方式
HttpPost httpPost = new HttpPost(url);
//设置消息头
httpPost.setHeader("Content-Type","application/json;charset=utf-8");
httpPost.setHeader("Accept","application/json");
//设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = null;
String result = "";
try {
httpResponse = httpClient.execute(httpPost);
//拿到实体
HttpEntity httpEntity= httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity,"utf-8");
}
//关闭连接
httpPost.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return result ;
}
public static String post(String url ,Map<String ,Object> map) {
//谷歌的Gson
Gson gson = new Gson();
//相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
HttpClient httpClient = HttpClients.createDefault();
//声明请求方式
HttpPost httpPost = new HttpPost(url);
//设置消息头
httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
httpPost.setHeader("X-Requested-With","XMLHttpRequest");
httpPost.setHeader("Authorization","YXBp");
//设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = null;
String result = "";
try {
httpResponse = httpClient.execute(httpPost);
//拿到实体
HttpEntity httpEntity= httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity,"utf-8");
}
//关闭连接
httpPost.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return result ;
}
public static String sendGet(String url,Map<String,String> headMap){
//相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
HttpClient httpClient = HttpClients.createDefault();
//声明请求方式
HttpGet httpGet = new HttpGet(url);
if(headMap!=null){
//设置消息头
for (String name : headMap.keySet()) {
httpGet.setHeader(name,headMap.get(name));
}
}
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = null;
String result = "";
try {
httpResponse = httpClient.execute(httpGet);
//拿到实体
HttpEntity httpEntity= httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity,"utf-8");
}
//关闭连接
httpGet.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return result ;
}
public static String sendPost(String url ,Map<String ,Object> map,Map<String,String> headMap){
//谷歌的Gson
Gson gson = new Gson();
//相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
HttpClient httpClient = HttpClients.createDefault();
//声明请求方式
HttpPost httpPost = new HttpPost(url);
//设置消息头
for (String name : headMap.keySet()) {
httpPost.setHeader(name,headMap.get(name));
}
//设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
//获取相应数据,这里可以获取相应的数据
HttpResponse httpResponse = null;
String result = "";
try {
httpResponse = httpClient.execute(httpPost);
//拿到实体
HttpEntity httpEntity= httpResponse.getEntity();
//获取结果,这里可以正对相应的数据精细字符集的转码
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity,"utf-8");
}
//关闭连接
httpPost.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return result ;
}
HTTPClientUtil工具类
最新推荐文章于 2024-07-10 09:39:47 发布