gpt4 book ai didi

java - 303 应答期间丢失 header

转载 作者:行者123 更新时间:2023-12-01 08:49:13 24 4
gpt4 key购买 nike

我有以下处理程序,它将我的请求重定向到另一个处理程序(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。我做错了什么?

提前致谢。

最佳答案

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

将您的位置更改为:

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

或将其添加为 SetCookie header :

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

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

关于java - 303 应答期间丢失 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com