title: Spring MVC proxy failing for Response with Content-Length
body: <p>I have wrote a proxy in my spring mvc service, which by passes any request that client making through a specific format to respective services. This is working fine, with some problems. Here is my code.  </p>
  
  <pre><code>        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&lt;byte[]&gt; responseEntity = restTemplate.exchange(uri, method, new HttpEntity&lt;Object&gt;(body,headers), byte[].class);
          return responseEntity;
  </code></pre>
  
  <p>This code breaks if the target server sends response with the following header. </p>
  
  <pre><code>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
  </code></pre>
  
  <p>And which is working fine if the target response is </p>
  
  <pre><code>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
  </code></pre>
  
  <p>The only difference I could Able to find the <em>Transfer-Encoding:chunked</em> and <em>Content-Length</em></p>
  
  <p>The proxy service is not transferring the whole data to the client. rather it breaks after getting some amount of data.</p>
  
  <p>If someone know what how can I fix it. I am sure its problem with the proxy configuration. But unable to fix it.</p>