如何在Spring MVC中代理HTTP请求?
我有一个基于Spring MVC构建的应用程序。
我想编写处理请求的简单代理,如下所示:
- 将相同的HTTP请求" title="HTTP请求">HTTP请求发送到某些特定的服务器
- 从此特定服务器捕获HTTP响应
- 向请求客户返回相同答案
这是到目前为止我得到的:
public void proxyRequest(HttpServletRequest request, HttpServletResponse response) { try {
HttpUriRequest proxiedRequest = createHttpUriRequest(request);
HttpResponse proxiedResponse = httpClient.execute(proxiedRequest);
writeToResponse(proxiedResponse, response);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeToResponse(HttpResponse proxiedResponse, HttpServletResponse response){
for(Header header : proxiedResponse.getAllHeaders()){
response.addHeader(header.getName(), header.getValue());
}
OutputStream os = null;
InputStream is = null;
try {
is = proxiedResponse.getEntity().getContent();
os = response.getOutputStream();
IOUtils.copy(is, os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private HttpUriRequest createHttpUriRequest(HttpServletRequest request) throws URISyntaxException{
URI uri = new URI(geoserverConfig.getUrl()+"/wms?"+request.getQueryString());
RequestBuilder rb = RequestBuilder.create(request.getMethod());
rb.setUri(uri);
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
rb.addHeader(headerName, headerValue);
}
HttpUriRequest proxiedRequest = rb.build();
return proxiedRequest;
}
可以,但是并非在所有情况下都可以。我已经检查了Chrome的网络监视器,并且使用此代理的某些请求失败。
以下是示例失败的请求响应的标头:
HTTP/1.1 200 OKServer: Apache-Coyote/1.1
Content-Disposition: inline; filename=JEDN_EWID.png
Transfer-Encoding: chunked
Date: Thu, 16 Jul 2015 10:31:49 GMT
Content-Type: image/png;charset=UTF-8
Content-Length: 6727
以下是示例成功请求响应的标头:
HTTP/1.1 200 OKServer: Apache-Coyote/1.1
Content-Disposition: inline; filename=JEDN_EWID.png
Transfer-Encoding: chunked
Date: Thu, 16 Jul 2015 10:31:49 GMT
Content-Type: image/png;charset=UTF-8
Transfer-Encoding: chunked
更重要的是,Chrome在控制台中引发错误:
GET http://localhost:8080/<rest of url> net::ERR_INVALID_CHUNKED_ENCODING
我正在代理的请求是WMS GetMap请求,我的代理正在将它们转发到隐藏的Geoserver。我注意到失败的请求应该返回透明的512x512
.png图像,这些图像都是空的。成功的图像返回512x512 .png图像,这些图像不仅透明而且还包含一些颜色。
回答:
看起来当大小变得太大时,远程服务器会以分块响应进行响应,Apache
HttpClient库将所有分块元素收集在一个大的HttpResponse中,但是保留了Transfer-Encoding: chunked
标头。
我无法测试,但是您应该过滤掉Transfer-Encoding: chunked
来摆脱此问题:
private void writeToResponse(HttpResponse proxiedResponse, HttpServletResponse response){ for(Header header : proxiedResponse.getAllHeaders()){
if ((! header.getName().equals("Transfer-Encoding")) || (! header.getValue().equals("chunked"))) {
response.addHeader(header.getName(), header.getValue());
}
}
...
以上是 如何在Spring MVC中代理HTTP请求? 的全部内容, 来源链接: utcz.com/qa/431615.html