【发布时间】:2012-01-29 12:35:26
【问题描述】:
我一直在尝试在 java 代码中处理重定向 (302),我终于能够做到。但我遇到了一个问题。也就是说,一旦重定向打开一个页面,点击页面上的任何链接都会让我回到登录页面。
所以我必须编写自己的重定向实现:
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;
}
根据我的想法,我可能不会重新发送或保留会话 cookie。我将如何重新发送或保留会话 cookie?以上代码如有错误,请赐教。
任何其他想法将不胜感激。
【问题讨论】:
-
为了增加您在此处获得响应的机会,请确保您的代码格式正确,并删除对阅读代码的人来说毫无意义的内容(例如过多的调试语句)。我刚刚为您完成了上述操作。
-
非常感谢..下次我会全力以赴的。
标签: java http redirect session-cookies apache-commons-httpclient