package com.itemutils.http.apache;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Map;
/**
- @Author zbf
- @DATA 2019/8/28 15:54
- @ClassIntroduction 使用apache httpclient进行封装的请求
- 基于apchehttpclient的封装
- 需要使用的jar 包
-
<dependency>
-
<groupId>org.apache.httpcomponents</groupId>
-
<artifactId>httpclient</artifactId>
-
<version>4.5.6</version>
-
</dependency>
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>fastjson</artifactId>
-
<version>1.2.47</version>
-
</dependency>
*/
public class UseHttp {
/**
* 请求类型
*/
JSON(“Content-Type”,“application/json”),
XML(“Content-Type”,“text/xml”),
/**
* get请求 不设置请求头
* @param url 请求地址
* @return
*/
public static String get(String url){
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = getAddHeader(url, null);
try {
HttpResponse execute = httpClient.execute(httpGet);
return EntityUtils.toString(execute.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* get 请求
* @param url 请求url
* @param header 请求头参数
* @return
*/
public static String get(String url,Map<String,String> header){
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = getAddHeader(url, header);
try {
HttpResponse execute = httpClient.execute(httpGet);
return EntityUtils.toString(execute.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param header 请求头信息
*/
private static HttpGet getAddHeader(String url ,Map<String, String> header){
HttpGet httpGet = new HttpGet(url);
if (null!=header && header.size()>0){
header.entrySet().forEach(e->httpGet.addHeader(e.getKey(),e.getValue()));
}else {
httpGet.addHeader(HttpType.JSON.getName(),HttpType.JSON.getType());
}
return httpGet;
}
/**
* post 请求 json格式
* @param url 请求地址
* @param header 请求头信息
* @param body 请求体中信息
* @return
*/
public static String post(String url,Map<String,String> header,Map<String,String> body){
return post(url,header,body,null, null);
}
/**
* post请求
* @param url 请求地址
* @param header 请求头中信息
* @param dataXml 请求参数为txt or xml
* @return
*/
public static String post(String url,Map<String,String> header,String dataXml){
return post(url,header,null,null, dataXml);
}
/**
* post 请求
* @param url 请求连接
* @param header 请求头的信息
* @param body 请求体参数
* @param bodyType 请求体的参数类型 默认是utf-8
* @return
*/
public static String post(String url,Map<String,String> header,Map<String,String> body,String bodyType,String txtXml){
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = postAddHeader(url,header);
try {
//设置请求参数的格式为json
if (header.get(HttpType.JSON.getName()).equals(HttpType.JSON.getType())){
httpPost.setEntity(new StringEntity(JSON.toJSONString(body),(bodyType==null ||"".equals(bodyType))?HttpConstant.UTF_8:bodyType));
}
//设置请求参数的类型格式xml
else if (header.get(HttpType.XML.getName()).equals(HttpType.XML.getType())){
httpPost.setEntity(new StringEntity(txtXml,(bodyType==null ||"".equals(bodyType))?HttpConstant.UTF_8:bodyType));
}
HttpResponse response = httpClient.execute(httpPost);
return response.getEntity().toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* post请求头的封装
* @param header 请求头信息
*/
private static HttpPost postAddHeader(String url ,Map<String, String> header){
HttpPost httpPost = new HttpPost(url);
if (null!=header && header.size()>0){
header.entrySet().forEach(e->httpPost.addHeader(e.getKey(),e.getValue()));
}else {
httpPost.addHeader(HttpType.JSON.getName(),HttpType.JSON.getType());
}
return httpPost;
}
}