Proxy servlet hangs on HttpClient.execute ()

I need to write a servlet that basically just proxies every incoming request to the same URL path on a different host. Here is what I came up with using Apache Commons Http Client 4.1.3:

@WebServlet("/data/*")
public class ProxyServlet extends HttpServlet {

  protected void doGet (HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    HttpClient client = new DefaultHttpClient();
    try {
      String url = getMappedServiceUrlFromRequest(request);
      HttpGet get = new HttpGet(url);
      copyRequestHeaders(request, get);

      HttpResponse getResp = client.execute(get);
      response.setStatus(getResp.getStatusLine().getStatusCode());
      copyResponseHeaders(getResp, response);

      HttpEntity entity = getResp.getEntity();
      if (entity != null) {
        OutputStream os = response.getOutputStream();
        try {
          entity.writeTo(os);
        } finally {
          try { os.close(); } catch (Exception ignored) { }
        }
      }
    } catch (Exception e) {
      throw new ServletException(e);
    } finally {
      client.getConnectionManager().shutdown();
    }
  }

  private void getMappedServiceUrlFromRequest (...)
  private void copyResponseHeaders (...)
  private void copyRequestHeaders (...)
}

This only works the first time the servlet is called. However, after the first time, the servlet hangs on the client.execute (get) line.

There are many Google hits for "HttpClient execute hangs", most of which suggest using an instance of ThreadSafeClientConnManager. Tried this, unfortunately, did not help.

I spent several hours looking for a problem, but I have not yet found anything to fix. I would seriously appreciate any guidance regarding what I am doing wrong here.

+3
source share
1

. , .

TCP-, . HTTP- -, CONNECT, HTTP, , - HTTP, . - .

0

All Articles