其他分享
首页 > 其他分享> > httpClientUtil的get请求

httpClientUtil的get请求

作者:互联网

这个是httpClient的get请求写法,一般需求应该是够用了。
参考了几个博客的代码,具体的网址忘记了,有问题可以联系我。
代码:
package com.demo.service.impl;

import com.demo.service.HttpRequestGetService;
import com.demo.utils.CloseHttpUtil;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;

@Component
public class HttpRequestGetServiceImpl implements HttpRequestGetService {
CloseHttpUtil closeHttpUtil = new CloseHttpUtil();

public String doGetRequest(String url, Map<String, String> headMap, Map<String, String> paramMap) {
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
/*
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
*/
URIBuilder uriBuilder = new URIBuilder(url);
for (Map.Entry<String, String> param : paramMap.entrySet()) {
uriBuilder.addParameter(param.getKey(), param.getValue());
}
// 根据带参数的URI对象构建GET请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build());

/*
* 添加请求头信息
*/
for (Map.Entry<String, String> header : headMap.entrySet()) {
httpGet.addHeader(header.getKey(), header.getValue());
}
/*httpGet.addHeader("Content-Type", "application/VIID+JSON;charset=utf8");
httpGet.addHeader("User-Identify","12345678905030000000");*/
closeHttpUtil.setTimeOut(httpGet);
// 执行请求
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
System.err.println("Http协议出现问题");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析错误");
e.printStackTrace();
} catch (URISyntaxException e) {
System.err.println("URI解析异常");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO异常");
e.printStackTrace();
} finally {
// 释放连接
closeHttpUtil.close(response, httpClient);
}

return entityStr;
}

public String doGetRequestNoparam(String url, Map<String, String> headMap) {
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
/*
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
*/
URIBuilder uriBuilder = new URIBuilder(url);
// 根据带参数的URI对象构建GET请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
/*
* 添加请求头信息
*/
for (Map.Entry<String, String> header : headMap.entrySet()) {
httpGet.addHeader(header.getKey(), header.getValue());
}
/*httpGet.addHeader("Content-Type", "application/VIID+JSON;charset=utf8");
httpGet.addHeader("User-Identify","12345678905030000000");*/
closeHttpUtil.setTimeOut(httpGet);
// 执行请求
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
System.err.println("Http协议出现问题");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析错误");
e.printStackTrace();
} catch (URISyntaxException e) {
System.err.println("URI解析异常");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO异常");
e.printStackTrace();
} finally {
// 释放连接
closeHttpUtil.close(response, httpClient);
}
return entityStr;
}

public String doGetRequestStringParam(String url, Map<String, String> headMap, String param) {
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
/*
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
*/
URIBuilder uriBuilder = new URIBuilder(url);
// 根据带参数的URI对象构建GET请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build() + param);
System.out.println("调用String参数的URL:" + httpGet);
/*
* 添加请求头信息
*/
for (Map.Entry<String, String> header : headMap.entrySet()) {
httpGet.addHeader(header.getKey(), header.getValue());
}
closeHttpUtil.setTimeOut(httpGet);
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
System.err.println("Http协议出现问题");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析错误");
e.printStackTrace();
} catch (URISyntaxException e) {
System.err.println("URI解析异常");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO异常");
e.printStackTrace();
} finally {
// 释放连接
closeHttpUtil.close(response, httpClient);

}
return entityStr;
}

public String doGetRequestIntParam(String url, Map<String, String> headMap, int RecordStartNo, int PageRecordNum) {
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
/*
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
*/
URIBuilder uriBuilder = new URIBuilder(url);
// 根据带参数的URI对象构建GET请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build() + "?" + "RecordStartNo=" + RecordStartNo + "&" + "PageRecordNum=" + PageRecordNum);
System.out.println("调用String参数的URL:" + httpGet);
/*
* 添加请求头信息
*/
for (Map.Entry<String, String> header : headMap.entrySet()) {
httpGet.addHeader(header.getKey(), header.getValue());
}
closeHttpUtil.setTimeOut(httpGet);
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
System.err.println("Http协议出现问题");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析错误");
e.printStackTrace();
} catch (URISyntaxException e) {
System.err.println("URI解析异常");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO异常");
e.printStackTrace();
} finally {
// 释放连接
closeHttpUtil.close(response, httpClient);
}
return entityStr;
}
}
__________________________________________________________________
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;

public class CloseHttpUtil {
public static void close(CloseableHttpResponse response, CloseableHttpClient httpClient) {
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
System.err.println("释放连接出错");
e.printStackTrace();
}
}
}

public static void setTimeOut(HttpGet httpGet) {
RequestConfig requestConfig = RequestConfig.custom()
// 设置连接超时时间(单位毫秒)
.setConnectTimeout(10000)
// 设置请求超时时间(单位毫秒)
.setConnectionRequestTimeout(10000)
// socket读写超时时间(单位毫秒)
.setSocketTimeout(10000)
// 设置是否允许重定向(默认为true)
.setRedirectsEnabled(true).build();

// 将上面的配置信息 运用到这个Get请求里
httpGet.setConfig(requestConfig);
}

标签:httpGet,请求,httpClientUtil,get,System,catch,println,import,response
来源: https://www.cnblogs.com/wangquanyi/p/11328799.html