简体   繁体   English

使用httpservletrequest拦截http帖子

[英]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. 我将把http post请求重定向到另一台服务器(只是更改url和一些标头),并为此使用Servlet。 In Servlet, i'm using HttpClient to send request and get response. 在Servlet中,我使用HttpClient发送请求并获取响应。 The question is: would these operations send the whole post request to the target? 问题是:这些操作是否会将整个发帖请求发送给目标? :

  1. copy httpServletRequest headers into httpClient request 将httpServletRequest标头复制到httpClient请求中

  2. and then put httpServletRequest#inputStream in request entity like this: 然后将httpServletRequest#inputStream放在请求实体中,如下所示:

    HttpEntity entity = new InputStreamEntity(httpServletRequest.getInputStream(), someContentType); HttpEntity实体= 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. 重定向是关于将请求发起者发送到另一个URL。 this is not what you are explaining. 这不是您要解释的。
  2. forward is about going to another internal url without request originator knowing anything. 转发是关于转到另一个内部URL,而请求发起者不知道任何内容。 from the point of view of originator, response is coming from the url it requested for. 从发起者的角度来看,响应来自其请求的URL。 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. 这是您的代码似乎通过使用诸如apache httpclient之类的特定框架所做的事情。 But you are trying to do so by reusing inputstream from original request, which you should not do. 但是您尝试通过重用原始请求中的inputstream来这样做,您不应该这样做。

if you want to add headers to the request, redirect is not an option afaik. 如果要向请求添加标头,则重定向不是afaik选项。 if you want to forward to another external url, forwarding is not an option. 如果要转发到另一个外部URL,则不能选择转发。

so, you need to do a new http request to the other site as part of your original request processing. 因此,作为原始请求处理的一部分,您需要向另一个站点发出新的http请求。 but you must process that original request to extract whatever info you need (not trying to reuse inputstream). 但是您必须处理该原始请求以提取所需的任何信息(不要尝试重用inputstream)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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