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.
Ahmed source share