在 Java 中重定向请求时如何在 HTTP 标头中传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24179361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:38:41  来源:igfitidea点击:

How to pass data in HTTP Header while redirecting a request in Java

javahttp-headersservlet-filtershttp-redirect

提问by anij

is it possible to pass some data in HTTP Header, while redirecting a request from one server to another.

是否可以在 HTTP 标头中传递一些数据,同时将请求从一台服务器重定向到另一台服务器。

Here is my scenario, I have one generic filter, via which every request is passing. Now, based on some condition, I'm redirecting the request to some different server using the API objHttpServletResponse.sendRedirect(strURL).

这是我的场景,我有一个通用过滤器,每个请求都通过它传递。现在,根据某些条件,我使用 API 将请求重定向到某个不同的服务器 objHttpServletResponse.sendRedirect(strURL)

But, the issue is, when I'm setting some data in response header like objHttpServletResponse.setHeader("Key", "Value");That's not available in the redirected server.

但是,问题是,当我在响应标头中设置一些数据时,例如objHttpServletResponse.setHeader("Key", "Value");在重定向服务器中不可用。

So, my questions are,

所以,我的问题是,

1. Is there any way to pass some data in header while redirecting a request?

1. 有没有什么办法可以在重定向请求时在头部传递一些数据?

2. If not, what are the other possible ways to send some data while redirecting a request?

2. 如果没有,在重定向请求的同时发送一些数据的其他可能方式是什么?

Please Note:few other ways, like

请注意:其他几种方式,例如

using URL parameters:objHttpServletResponse.sendRedirect(strURL+"?param="+ strParamValue);

使用 URL 参数:objHttpServletResponse.sendRedirect(strURL+"?param="+ strParamValue);

or

或者

using session:HttpSession session = httpRequest.getSession(); session.setAttribute("Key", "Value");

使用会话:HttpSession session = httpRequest.getSession(); session.setAttribute("Key", "Value");

is not what I'm expecting.

不是我所期待的。

采纳答案by flup

The headers you set are written to the response that gets sent to the client, along with a Location header and a status code. See Redirecting a request using servlets and the "setHeader" method not working

您设置的标头与位置标头和状态代码一起写入发送到客户端的响应。请参阅使用 servlet 重定向请求和“setHeader”方法不起作用

The client is then supposed to send an identical request to the URL you specified in the Location header. Identical to the request it sent to you.

然后,客户端应该向您在 Location 标头中指定的 URL 发送相同的请求。与它发送给您的请求相同。

You want the browser to send a header you specify along with the redirected request. Have you considered adding a (domain) Cookie header? Some googling leads me to believe that cookies set in a redirect response will get picked up by most browsers. See http://blog.dubbelboer.com/2012/11/25/302-cookie.html

您希望浏览器将您指定的标头与重定向请求一起发送。您是否考虑过添加(域)Cookie 标头?一些谷歌搜索让我相信在重定向响应中设置的 cookie 会被大多数浏览器接收。见http://blog.dubbelboer.com/2012/11/25/302-cookie.html

回答by Sergey Alaev

You can use JS redirect, i.e. instead of calling sendRedirectreturn HTML page with embedded javascript that will do redirect setting headers you need.

您可以使用 JS 重定向,即而不是调用sendRedirect带有嵌入式 javascript 的返回 HTML 页面,该页面将执行您需要的重定向设置标头。

However, using GET parameters is really the best solution. If you have concerns about users altering parameters manually - use MAC code to protect parameters.See Message authentication code

但是,使用 GET 参数确实是最好的解决方案。如果您担心用户手动更改参数 - 使用 MAC 码来保护参数。请 参阅消息验证码

In simplest form, ?p1=1&p2=2&mac={mac value}, where {mac value} = md5('MY_SECRET_KEY' + 'p1=1&p2=2').Receiving side can recalculate MAC and compare it with provided one. Since external users can not know 'MY_SECRET_KEY', they will not be able to make valid MAC.

在最简单的形式中,?p1=1&p2=2&mac={mac value}, where {mac value} = md5('MY_SECRET_KEY' + 'p1=1&p2=2').接收方可以重新计算 MAC 并将其与提供的进行比较。由于外部用户无法知道 'MY_SECRET_KEY',他们将无法制作有效的 MAC。

回答by SVashisth

Have you checked the HTTP request/response from/to server? You can use a number of plugins on chrome or firefox to check that. You would be able to see if value is being passed from your server to another server or not
Also retrieve the header using httpResponse.getHeader("Key"); not using request.getHeader("key"). One of my colleague was facing same issue some days back, he was using request to fetch header values

您是否检查过来自/发送到服务器的 HTTP 请求/响应?您可以在 chrome 或 firefox 上使用许多插件来检查。您将能够查看值是否正在从您的服务器传递到另一台服务器
还使用 httpResponse.getHeader("Key"); 检索标头;不使用 request.getHeader("key")。几天前我的一位同事面临同样的问题,他正在使用请求来获取标头值

回答by SparkOn

Please have a look at Apache HttpClient.

请查看Apache HttpClient

This example adds several parameters to the post request :

此示例向 post 请求添加了几个参数:

    String url = "https://selfsolve.apple.com/wcResults.do";

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("sn", "C02G8416DRJM"));
    urlParameters.add(new BasicNameValuePair("cn", ""));
    urlParameters.add(new BasicNameValuePair("locale", ""));
    urlParameters.add(new BasicNameValuePair("caller", ""));
    urlParameters.add(new BasicNameValuePair("num", "12345"));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    System.out.println("Response Code : " 
                + response.getStatusLine().getStatusCode());

回答by Adindu Stevens

The problem is that the redirect()method of the responseinitiates a new request altogether, thereby loosing the attributes that were set before redirecting. Luckily there is a fluent way of solving the problem still.

问题在于 的redirect()方法response完全发起了一个新的请求,从而失去了在重定向之前设置的属性。幸运的是,仍然有一种流畅的方法可以解决这个问题。

response.setHeader("Key", "Value");
request.getRequestDispatcher("redirecturl").forward(request, response);

Then in your destination you can do

然后在你的目的地你可以做

response.getHeaders("key")