Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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).

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.

So, my questions are,

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

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

Please Note: few other ways, like

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

or

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

is not what I'm expecting.

like image 755
anij Avatar asked Jun 12 '14 08:06

anij


People also ask

How do I pass HTTP request headers?

In the Name field, enter the name of your header rule (for example, My header ). From the Type menu, select Request, and from the Action menu, select Set. In the Destination field, enter the name of the header affected by the selected action. In the Source field, enter where the content for the header comes from.

Can you add headers to a redirect?

It's impossible to redirect to a page with custom headers set, no matter what language or framework you use. In other words, there's no way to trigger an HTTP redirect and cause the client (browser) to add a custom header. You might be thinking that using multiple header() calls should work just fine. But it won't.

What happens to headers on redirect?

HTTP headers are name and value pairs that are returned in responses from a Web server. Unlike custom headers, which are returned in every response from a Web server, redirect headers are returned only when redirection occurs.

Can we pass header GET request?

For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host. Your load balancer intercepts traffic between the client and your server.


2 Answers

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

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.

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

like image 147
flup Avatar answered Oct 09 '22 12:10

flup


Please have a look at Apache HttpClient.

This example adds several parameters to the post request :

    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());
like image 25
SparkOn Avatar answered Oct 09 '22 10:10

SparkOn