实现过程
- 获取请求地址
- 获取请求头
- 获取请求header
- 创建请求
- 执行请求
- 复制返回流
- 进行返回
private void httpProxy(HttpServletResponse response, String url,String methodName ) throws IOException {
HttpMethod httpMethod = HttpMethod.resolve(methodName);
if(httpMethod == null) {
return;
}
ClientHttpRequest delegate = new SimpleClientHttpRequestFactory().createRequest(URI.create(url), httpMethod);
ClientHttpResponse clientHttpResponse = delegate.execute();
response.setStatus(clientHttpResponse.getStatusCode().value());
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> response.setHeader(key, it)));
StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
}