HttpClient发起请求,将响应结果(header和entity)设置到response中返回
2021-01-03 08:27
标签:details method blank nio sso header creat custom lis HttpClient发起请求,将响应结果(header和entity)设置到response中返回 HttpClient发起请求,将响应结果(header和entity)设置到response中返回 标签:details method blank nio sso header creat custom lis 原文地址:https://www.cnblogs.com/yadongliang/p/13653323.html前言
package com.graph.oss.config;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.graph.atlas.common.base.util.JSONUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import org.apache.tomcat.util.buf.MessageBytes;
/**
* 对象服务接口透传 只有非控制接口(/oss/*)才会进到这个拦截器
*/
public class ContentInterceptor extends HandlerInterceptorAdapter {
private static final String PATH_DELIMITER = "/";
private OssServerConfig ossServerConfig;
private MinioServerConfig minioServerConfig;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// get配置文件bean
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
ossServerConfig = (OssServerConfig) factory.getBean("ossServerConfig");
minioServerConfig = (MinioServerConfig) factory.getBean("minioServerConfig");
// get request uri(undertow)
String uri = request.getRequestURI();
// uri处理(去掉context.api)
String api = ossServerConfig.getApi();
if (uri.startsWith(api)) {
uri = uri.replace(api, PATH_DELIMITER);
}
// get httpclient response
String minioUrl = minioServerConfig.getEndpoint() + uri;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(minioUrl);
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(1000)
.setSocketTimeout(10 * 1000).build();
httpGet.setConfig(config);
httpGet.addHeader("content-type", "application/json");
CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpGet);
ServletOutputStream out = null;
try {
// 将httpClient响应的header和entity设置到httpServletResponse
Header header = closeableHttpResponse.getFirstHeader("Content-Type");
response.setHeader(header.getName(), header.getValue());
HttpEntity entity = closeableHttpResponse.getEntity();
out = response.getOutputStream();
entity.writeTo(out);
out.flush();
out.close();
return false;
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
// 根据Field获得对应的Class
private Class getClassByName(Class classObject, String name) {
Map
感谢
https://blog.csdn.net/GY325416/article/details/81436078
HttpClient详细使用示例
文章标题:HttpClient发起请求,将响应结果(header和entity)设置到response中返回
文章链接:http://soscw.com/index.php/essay/39772.html