调用 request.getReader() 后重置 HttpRequest

发布于 2024-11-15 05:13:31 字数 163 浏览 2 评论 0原文

有没有办法在 HttpRequest 上调用 getReader() 方法,然后“重置”请求,以便对 getReader() 的其他调用将不抛出java.lang.IllegalStateException

Is there a way to call the getReader() method on a HttpRequest and then "reset" the request so other calls to getReader() will not throw java.lang.IllegalStateException?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-11-22 05:13:31

就像其他人所说的那样,不,我认为没有办法重置请求。

我也遇到过同样的情况,希望在继续之前将请求的内容记录在 ServletFilter 中。这是论坛帖子,帮助我弄清楚如何创建一个像 Stephen C 所说的那样的 HttpServletRequestWrapper 。这将存储数据,以便您可以多次调用 getReader()getInputStream()

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class MyRequestWrapper extends HttpServletRequestWrapper {

    private HttpServletRequest original;
    private byte[] reqBytes;
    private boolean firstTime = true;

    public MyRequestWrapper(HttpServletRequest request) {
        super(request);
        original = request;
    }

    @Override
    public BufferedReader getReader() throws IOException{

        if(firstTime)
            firstTime();

        InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(reqBytes));
        return new BufferedReader(isr);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {

        if(firstTime)
            firstTime();

        ServletInputStream sis = new ServletInputStream() {
            private int i;

            @Override
            public int read() throws IOException {
                byte b;
                if(reqBytes.length > i)
                    b = reqBytes[i++];
                else
                    b = -1;

                return b;
            }
        };

        return sis;
    }

    private void firstTime() throws IOException{
        firstTime = false;
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = original.getReader();
        String line;
        while((line = reader.readLine()) != null){
            buffer.append(line);
        }
        reqBytes = buffer.toString().getBytes();
    }
}

Like other people have said, No I don't think there is a way to reset the request.

I have been in the same situation of wanting to log the content of the request in a ServletFilter before moving on. Here is a forum post that helped me figure out how to create a HttpServletRequestWrapper like Stephen C was talking about. That will store the data so you can call getReader() and getInputStream() multiple times.

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class MyRequestWrapper extends HttpServletRequestWrapper {

    private HttpServletRequest original;
    private byte[] reqBytes;
    private boolean firstTime = true;

    public MyRequestWrapper(HttpServletRequest request) {
        super(request);
        original = request;
    }

    @Override
    public BufferedReader getReader() throws IOException{

        if(firstTime)
            firstTime();

        InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(reqBytes));
        return new BufferedReader(isr);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {

        if(firstTime)
            firstTime();

        ServletInputStream sis = new ServletInputStream() {
            private int i;

            @Override
            public int read() throws IOException {
                byte b;
                if(reqBytes.length > i)
                    b = reqBytes[i++];
                else
                    b = -1;

                return b;
            }
        };

        return sis;
    }

    private void firstTime() throws IOException{
        firstTime = false;
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = original.getReader();
        String line;
        while((line = reader.readLine()) != null){
            buffer.append(line);
        }
        reqBytes = buffer.toString().getBytes();
    }
}
挖鼻大婶 2024-11-22 05:13:31

简单的答案是“否”。

该流不可重置,并且没有 API 方法允许您重新打开它。 (并且有充分的理由。它需要 servlet 基础设施保留输入的副本以防万一 servlet 决定重新打开流。这将是不必要的开销。)

如果您想要这样做这种事情,您将需要编写代码来保留自己的数据副本。如果您在 Filter(或 Tomcat Valve)中实现此功能,那么您可以创建一个 HttpServletRequestWrapper 来隐藏您已经读取数据的事实......正如 @Vineet 所建议的那样。

The simple answer is "No".

The stream is not resettable and there's no API method that will allow you to reopen it. (And for good reason. It would require the servlet infrastructure to keep a copy of the input just in case the servlet decided to reopen the stream. That would be an unwarranted overhead.)

If you want to do this kind of thing, you will need to write your code to keep its own copy of the data. If you are implementing this in a Filter (or a Tomcat Valve) then you could create a HttpServletRequestWrapper to hide the fact that you've already read the data .... as suggested by @Vineet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文