我在我的 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
我能找到传输编码的唯一区别:分块和内容长度
代理服务不会将整个数据传输到客户端。而是在获取一些数据后中断。
如果有人知道我该如何解决它。我确信它的代理配置有问题。但无法修复它。