简体   繁体   中英

intercepting http post using httpservletrequest

I'm going to redirect http post request to another server (just changing url and some headers) and using a Servlet for this purpose. In Servlet, i'm using HttpClient to send request and get response. The question is: would these operations send the whole post request to the target? :

  1. copy httpServletRequest headers into httpClient request

  2. and then put httpServletRequest#inputStream in request entity like this:

    HttpEntity entity = new InputStreamEntity(httpServletRequest.getInputStream(), someContentType);

I mean is the request = headers + input stream? if not, what else should be copied?

Just to be on same page regarding terminology:

  1. redirect is about sending the request originator to another url. this is not what you are explaining.
  2. forward is about going to another internal url without request originator knowing anything. from the point of view of originator, response is coming from the url it requested for. this is not what your sample code is trying to do
  3. your code doing reuqests elsewhere as part of the originator request processing. this is what your code seems to be doing by using specific frameworks like apache httpclient. But you are trying to do so by reusing inputstream from original request, which you should not do.

if you want to add headers to the request, redirect is not an option afaik. if you want to forward to another external url, forwarding is not an option.

so, you need to do a new http request to the other site as part of your original request processing. but you must process that original request to extract whatever info you need (not trying to reuse inputstream).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM