【问题标题】:Lost Header during 303 answer在 303 回答期间丢失标题
【发布时间】:2017-07-18 06:50:44
【问题描述】:

我有以下处理程序,它将我的请求重定向到另一个处理程序(LoginHandler):

public class ValidationHandler implements HttpHandler
{
    @Override
    public void handle(HttpExchange httpExchange) throws IOException
    {
       // Serve for POST requests only
       if (httpExchange.getRequestMethod().equalsIgnoreCase("POST"))
       {  
           Headers hds = httpExchange.getResponseHeaders();
           hds.add("Content-type","text/html");
           hds.add("Location","/login");
           //WHERE I ADD THE USER
           hds.add("User", "someUser");
           //SEND A 303 to go to another handler (LoginHandler)
           httpExchange.sendResponseHeaders(303,0);  
           httpExchange.close();
       }
   }
}

这是下面的Header,LoginHeader:

public class LoginHandler implements HttpHandler
{

   @Override
   public void handle(HttpExchange httpExchange) throws IOException
   {
        //I can't read the header USER on the following line!
       httpExchange.getRequestHeaders().forEach((k,v) -> System.out.println(k + ':' + v));

        httpExchange.sendResponseHeaders(200,data.length());
        httpExchange.getResponseBody().write(data.getBytes());
        httpExchange.close();
   }
}

我无法在 requestHeaders 上读取我添加的 Header USER。 我做错了什么?

提前致谢。

【问题讨论】:

    标签: java http http-headers http-status-code-303


    【解决方案1】:

    响应标头不遵循 http 重定向。您可以将其作为 cookie 或作为查询参数包含到 Location 标头中。

    将您的位置更改为:

    hds.add("Location","/login?User=someUser");
    

    或将其添加为 SetCookie 标头:

    hds.add("Set-Cookie", "User=someuser")
    

    Set-Cookie 将告诉浏览器存储 cookie 并将其包含在所有返回服务器的请求中。将其包含在您的位置中将使其在处理重定向时可以访问一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2014-11-04
      • 2011-07-27
      • 1970-01-01
      • 2012-04-19
      • 2014-08-31
      相关资源
      最近更新 更多