How to resend or save a session cookie

I am trying to handle a redirect (302) in Java code, and finally I was able to do this. But I ran into a problem. If a page opens after redirecting, clicking on any link on the page returns me to the login page.

Therefore, I have to write my own redirect implementation:

private HttpMethod loadHttp302Request(HttpMethod method, HttpClient client, int status, String urlString) throws HttpException, IOException { if (status != 302) return null; String[] url = urlString.split("/"); HttpMethod theMethod = new GetMethod(urlString + method.getResponseHeader("Location") .getValue()); theMethod.setRequestHeader("Cookie", method.getResponseHeader("Set-Cookie") .getValue()); theMethod.setRequestHeader("Referrer", url[0] + "//" + url[2]); theMethod.setDoAuthentication(method.getDoAuthentication()); theMethod.setFollowRedirects(method.getFollowRedirects()); int _status = client.executeMethod(theMethod); return theMethod; } 

In my opinion, I cannot resend or save the session cookie. How can I resubmit or save a session cookie? If there are any errors in the above code, please enlighten me.

Any other ideas would be appreciated.

0
source share
1 answer

The most likely problem is that you seem to think that the final assignment in your method ( method = theMethod ) has an effect outside of loadHttp302Request . ( edit : the source code had this statement, but OP changed it later)

This is not true.

Java does not have semantics on demand, so an assignment does not have a pure effect. If you want to save the answer (most importantly, cookie) for the next call, you need to return theMethod and use it the next time. Sort of:

 private HttpMethod loadHttp302Request(HttpMethod method, HttpClient client, int status, String urlString) throws HttpException, IOException { // code as before return theMethod; } 
0
source

Source: https://habr.com/ru/post/1388211/


All Articles