1.场景描述
使用Apache开源组织中的HttpComponents,完成对http服务器的访问功能,消息体xml报文。
2.HttpComponents项目的介绍
HttpComponents项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。通过它可以让原来很头疼的事情现在轻松的解决,例如你不再管是HTTP或者HTTPS的通讯方式,告诉它你想使用HTTPS方式,剩下的事情交给 httpclient替你完成。
3.maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
4.代码编写
工具类
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; /**
* apache httpclient 工具类
* 支持对 http https 接口调用
* <P>
* 1.GET 请求
* 2.POST 请求
* </P>
*/
public class HttpDelegate {
private int timeOut = 10000;//指定超时时间
private CloseableHttpClient httpClient;
private RequestConfig requestConfig;//设置请求参数 /**
* 对象被创建时执行
*/
public HttpDelegate(Boolean isHttps){
httpClient = this.getHttpClient(isHttps);
this.initRequestConfig();//初始化请求配置
} /**
* 发送post请求
* @param url 请求地址
* @param contentType 请求类型
* @param encoding 编码格式
* @param param 请求参数
* @return
*/
public String executePost(String url,String contentType,String encoding,String param)
{
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setConfig(requestConfig);
this.initMethodParam(httpPost, contentType, encoding);
if(param!=null)
{
httpPost.setEntity(new StringEntity(param, encoding));//设置请求体
}
return this.execute(httpPost,httpClient);
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage(), e);
}
finally
{
httpPost.releaseConnection();
}
} /**
* 发送get请求
* @param url 请求地址
* @param contentType 请求类型
* @param encoding 编码格式
* @return
*/
public String executeGet(String url,String contentType,String encoding){
HttpGet httpGet = new HttpGet(url);
try{
httpGet.setConfig(requestConfig);
this.initMethodParam(httpGet, contentType, encoding);
return this.execute(httpGet,httpClient);
}catch (Exception e){
throw new RuntimeException(e.getMessage(),e);
}finally{
httpGet.releaseConnection();
}
}
/**
* 通用方法,用来发送post或get请求
* @param method 请求类型
* @param httpClient
* @return
* @throws RuntimeException
* @throws IOException
*/
private String execute(HttpUriRequest method,CloseableHttpClient httpClient) throws RuntimeException,IOException{
CloseableHttpResponse response = null;
try{
response = httpClient.execute(method);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, method.getFirstHeader(HttpHeaders.CONTENT_ENCODING).getValue());
}catch (Exception e){
throw new RuntimeException(e.getMessage(), e);
}finally{
if(response != null){
response.close();
}
}
} private void initMethodParam(HttpUriRequest method, String contentType, String encoding){
if (contentType != null){
method.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
}
method.setHeader(HttpHeaders.CONTENT_ENCODING, encoding);
method.setHeader(HttpHeaders.TIMEOUT, "60000");
} /**
* 设置初始值
*/
private void initRequestConfig() {
requestConfig=RequestConfig.custom().setSocketTimeout(timeOut).setConnectTimeout(timeOut).build();
} /**
* 获取主体类:
* isHttps 区分https 和 https
* @param isHttps
* @return
*/
private CloseableHttpClient getHttpClient(boolean isHttps) {
CloseableHttpClient chc = null;
try{
if (isHttps){
TrustManager[] trustManagers = new TrustManager[1];
trustManagers[0] = new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException{}
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException{}
public X509Certificate[] getAcceptedIssuers(){
return null;
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(new KeyManager[0], trustManagers, new SecureRandom());
SSLContext.setDefault(sslContext);
sslContext.init(null, trustManagers, null);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
chc = HttpClients.custom().setSSLSocketFactory(sslsf).build();
}else{
chc=HttpClients.custom().build();
}
}catch (Exception e){
throw new RuntimeException(e.getMessage(), e);
}
return chc;
} /**
* 关闭链接
*/
public void destroy(){
System.out.println("方法被执行");
try{
httpClient.close();
}catch (IOException e){
throw new RuntimeException(e.getMessage(),e);
}
}
}
服务端接口
@RequestMapping(value="/txp/test.action",method= RequestMethod.POST)
public void testHttpPost(HttpServletRequest request, HttpServletResponse response) throws Exception{
//解析请求报文
request.setCharacterEncoding("UTF-8"); //返回页面防止出现中文乱码
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));//post方式传递读取字符流
String jsonStr = null;
StringBuilder sb = new StringBuilder();
try {
while ((jsonStr = reader.readLine()) != null) {
sb.append(jsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
reader.close();// 关闭输入流
System.out.println("请求报文:"+sb.toString());
String result = "true";
response.getWriter().print(result);
}
测试方法
@Test
public void test(){
//组装回调参数
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\"?>");
sb.append("<root>");
sb.append("<param1>").append("hello word").append("</param1>");
sb.append("<param2>").append("!").append("</param2>");
sb.append("</root>"); //开始进行回调
log.info("回调信息,{}",sb.toString());
String responseBody = hd.executePost("http://localhost:7082/ecurrency-cardtransfer/txp/test.action", "text/xml", "GBK", sb.toString());
System.out.println("返回信息:"+responseBody);
}