如何在转发请求体之前修改它

时间:2015-12-07 14:01:53

标签: java servlets

我想在HttpPost中执行相同的功能,使用servlet,而不是使用HttpPost创建请求,我想使用来自servlet的另一个请求并在将其转发到URL之前更改主体" www.url.com/cgi-bin" ;,如何更改请求的正文内容?

public void call() throws ClientProtocolException, IOException, InterruptedException {

        String url = "www.url.com/cgi-bin"
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost httppost = new HttpPost(url);
        String data = "body data";
        InputStream stream = new ByteArrayInputStream(data.getBytes("UTF-8"));
        InputStreamEntity reqEntity = new InputStreamEntity(stream, -1);
        reqEntity.setChunked(true);
        httppost.setEntity(reqEntity);
        httppost.addHeader("charset", "utf-8");
        httppost.setHeader("Content-Type", "text/xml");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        httpclient.getConnectionManager().shutdown();
    }

我希望它像......

@WebServlet("/myServlet/*")
    public class MyHandler extends HttpServlet {

        public void doGet(HttpServletRequest request,HttpServletResponse response) {
            // add data to request here ...
            // forward request to the URL ...
        }
    }

1 个答案:

答案 0 :(得分:0)

不幸的是,使用servlet api,servlet无法生成带有正文内容的新帖子请求。