springspring-mvc

Forward HttpServletRequest to a different server


I got a HttpServletRequest request in my Spring MVC controller which I would like to forward AS-IS (i.e. GET or POST content) to a different server.

What would be the best way to do it using Spring Framework?

Do I need to grab all the information and build a new HTTPUrlConnection? Or there is an easier way?


Solution

  • Unfortunately there is no easy way to do this. Basically you'll have to reconstruct the request, including:

    That's a lot of work, not to mention it won't scale since each such proxy call will occupy one thread on your machine.

    My advice: use raw sockets or and intercept HTTP protocol on the lowest level, just replacing some values (like Host header) on the fly. Can you provide more context, why so you need this?