Spring MVC代理因Content-Length响应失败

时间:2015-11-26 07:04:18

标签: java spring-mvc http-proxy transfer-encoding http-content-length

我在我的spring mvc服务中编写了一个代理,它通过将客户端通过特定格式发出的任何请求传递给各自的服务。这工作正常,有一些问题。这是我的代码。

        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        InetSocketAddress address = new InetSocketAddress(this.serviceUrl, Integer.parseInt(this.servicePort));
        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
        factory.setProxy(proxy);
        factory.setBufferRequestBody(true);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(factory);

        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(uri, method, new HttpEntity<Object>(body,headers), byte[].class);
        return responseEntity;

如果目标服务器使用以下标头发送响应,则此代码会中断。

Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:3636
Content-Type:application/json
Date:Thu, 26 Nov 2015 06:51:26 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.9 (Unix) PHP/5.5.23
X-Powered-By:PHP/5.5.23

如果目标响应是

,哪个工作正常
Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:application/json
Date:Thu, 26 Nov 2015 06:54:14 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.9 (Unix) PHP/5.5.23
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.23

唯一的区别是我能够找到转移编码:分块内容长度

代理服务未将整个数据传输到客户端。而是在获得一些数据后它会中断。

如果有人知道我该怎么解决它。我确定它与代理配置有关。但无法修复它。

0 个答案:

没有答案