如何在Servlet中检查标头后删除请求的主体 [英] How to drop body of a request after checking headers in Servlet

查看:148
本文介绍了如何在Servlet中检查标头后删除请求的主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在继续使用正文之前检查请求的标头是否包含某个标头。例如,我想检查一个multipart / form-data是否在标题中包含Authorization。如果不是,那么就不需要继续上传通常用于文件上传的多部分主体。

I want to check the header of the request whether it contains a certain header or not before continuing with the body. For example, I want to check whether a multipart/form-data contains "Authorization" in the header or not. If it is not then there is no need to continue with uploading the multipart body which are generally quite large for file uploading.

servlet是否允许你这样做?我试图随机搜索谷歌,但没有运气。这是我在我的servlet中尝试的代码,但在调用 doPost 方法之前,它仍然继续接收正文。似乎在调用servlet之前完全接收了流。

Does servlet allow you to do this? I have tried to search on google randomly but there is no luck. Here is the code i try in my servlet but it still continues with recieving the body before this doPost method is called. It seems that the stream is fully received before the servlet is invoked.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    response.setContentType("text/plain");
    if (request.getHeader("Authorization") == null) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        out.println("Status: " + HttpServletResponse.SC_UNAUTHORIZED + " - UNAUTHORIZED");
        return;
    }
    // ... the rests
}


推荐答案

这是HTTP的限制。如果未完全阅读请求,则无法发送回复。

That's the limitation of HTTP. You can't send a response when the request hasn't been read fully to end.

这篇关于如何在Servlet中检查标头后删除请求的主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆