HttpClient工具类
HttpClient工具类
1.maven依赖
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
2.代码
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.druid.util.StringUtils;
@Service
public class HttpClientService {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientService.class);
@Autowired(required=false)
private CloseableHttpClient httpClient;
@Autowired(required=false)
private RequestConfig requestConfig;
public String doGet(String url,Map<String,String> params,String charset) {
LOGGER.info("doGet");
String result = null;
//1.判断字符集编码是否为空 如果为空则给定默认值utf-8
if(StringUtils.isEmpty(charset)) {
charset="UTF-8";
}
//2.判断用户是否需要传递参数
if(params!=null&¶ms.size()!=0) {
try {
URIBuilder uriBuilder =new URIBuilder(url);
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
url=uriBuilder.build().toString();
} catch (Exception e) {
e.printStackTrace();
}
}
//3.定义参数提交对象
HttpGet get = new HttpGet(url);
//4.为请求设定超时时间
get.setConfig(requestConfig);
//5.通过httpClient发送请求
try {
CloseableHttpResponse response = httpClient.execute(get);
//表示程序调用成功
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode==200) {
result=EntityUtils.toString(response.getEntity(), charset);
}else {
System.out.println("调用异常:状态信息:"+statusCode);
throw new RuntimeException();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String doGet(String url){
return doGet(url, null, null);
}
public String doGet(String url,Map<String,String> params){
return doGet(url, params, null);
}
public String doGet(String url,String charset){
return doGet(url, null, charset);
}
/**
* post请求 的
* @param url
* @param params
* @param charset
* @return
*/
public String doPost(String url,Map<String,String> params,String charset) {
String result=null;
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
if(StringUtils.isEmpty(charset)) {
charset="UTF-8";
}
if(params!=null&¶ms.size()!=0) {
//构建表单提交的实例化对象
List<NameValuePair> parameters = new ArrayList<>();
for (Map.Entry<String,String> entry : params.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
try {
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, charset);
post.setEntity(formEntity);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
CloseableHttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode==200) {
result =EntityUtils.toString(response.getEntity(),charset );
}else {
System.out.println("调用异常:状态信息:"+statusCode);
throw new RuntimeException();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String doPost(String url,Map<String,String> params) {
return doPost(url,params,null);
}
}