1.POST方式
public static void main(String[] args) throws UnsupportedEncodingException, IOException{
Map<String, Object> map = new HashMap<String, Object>();
map.put("ceshi", "1");
Map<String, String> list=new HashMap<String, String>();
list.put("date",token);
String RepairListForApp = httpPost("http://",list, map);
System.out.println(RepairListForApp);
}
public static String httpPost(String url,Map<String, String> header,Map map){
// 返回body
String body = null;
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse=null;
// 2、创建一个HttpPost请求
HttpPost post = new HttpPost(url);
// 5、设置header信息
/**header中通用属性*/
post.setHeader("Accept","*/*");
post.setHeader("Accept-Encoding","gzip, deflate");
post.setHeader("Cache-Control","no-cache");
post.setHeader("Connection", "keep-alive");
post.setHeader("Content-Type", "application/json;charset=UTF-8");
/**业务参数*/
if (header!=null) {
Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
post.setHeader(entry.getKey(), entry.getValue());
}
}
// 设置参数
if (map != null) {
try {
StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
entity1.setContentEncoding("UTF-8");
entity1.setContentType("application/json");
post.setEntity(entity1);
// 7、执行post请求操作,并拿到结果
httpResponse = httpClient.execute(post);
// 获取结果实体
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return body;
}
2.get方式
public static void main(String[] args) throws UnsupportedEncodingException, IOException{
Map<String, Object> map = new HashMap<String, Object>();
map.put("ceshi", "1");
Map<String, String> list=new HashMap<String, String>();
list.put("date",token);
String RepairListForApp = httpGet("http://", list);
System.out.println(RepairListForApp);
}
public static String httpGet(String url, Map<String, String> header){
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse=null;
String finalString = null;
HttpGet httpGet = new HttpGet(url);
/**公共参数添加至httpGet*/
/**header中通用属性*/
httpGet.setHeader("Accept","*/*");
httpGet.setHeader("Accept-Encoding","gzip, deflate");
httpGet.setHeader("Cache-Control","no-cache");
httpGet.setHeader("Connection", "keep-alive");
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
/**业务参数*/
if (header!=null) {
Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
finalString= EntityUtils.toString(entity, "UTF-8");
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return finalString;
}
3.delete方式
public static void main(String[] args) throws UnsupportedEncodingException, IOException{
Map<String, String> list=new HashMap<String, String>();
list.put("date",token);
String RepairListForApp = httpdel("http://", list,null);
System.out.println(RepairListForApp);
}
public static String httpdel(String url,Map<String, String> header,Map map){
// 返回body
String body = null;
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse=null;
String finalString = null;
// 2、创建一个HttpPost请求
HttpDelete httpGet = new HttpDelete(url);
/**公共参数添加至httpGet*/
/**header中通用属性*/
httpGet.setHeader("Accept","*/*");
httpGet.setHeader("Accept-Encoding","gzip, deflate");
httpGet.setHeader("Cache-Control","no-cache");
httpGet.setHeader("Connection", "keep-alive");
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
/**业务参数*/
if (header!=null) {
Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
if(map!=null){
StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
((HttpResponse) httpGet).setEntity(entity1);
}
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
finalString= EntityUtils.toString(entity, "UTF-8");
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return finalString;
}
4.put方式
public static void main(String[] args) throws UnsupportedEncodingException, IOException{
Map<String, String> list=new HashMap<String, String>();
list.put("date",token);
String RepairListForApp = httpPut("http://", list,null);
System.out.println(RepairListForApp);
}
public static String httpPut(String url,Map<String, String> header,Map map){
// 返回body
String body = null;
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse=null;
String finalString = null;
// 2、创建一个HttpPost请求
HttpPut httpGet = new HttpPut(url);
/**公共参数添加至httpGet*/
/**header中通用属性*/
httpGet.setHeader("Accept","*/*");
httpGet.setHeader("Accept-Encoding","gzip, deflate");
httpGet.setHeader("Cache-Control","no-cache");
httpGet.setHeader("Connection", "keep-alive");
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
/**业务参数*/
if (header!=null) {
Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = it.next();
System.out.println(entry.getKey()+":"+entry.getValue());
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
if(map!=null){
StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
httpGet.setEntity(entity1);
}
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
finalString= EntityUtils.toString(entity, "UTF-8");
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return finalString;
}