如何重新发送或保留会话 cookie

发布于 2024-12-23 06:17:42 字数 997 浏览 1 评论 0原文

我一直在尝试在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?以上代码如有错误,请多多指教。

任何其他想法将不胜感激。

I have been trying to handle a redirect (302) in java code and I am finally been able to do it. But I am running into a problem. Which is, once the redirect opens a page, clicking any link on the page sends me back to the login page.

So 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;
}

According to my thinking I might not be re-sending or retaining the session cookie. How will I be able to do resend or retain the session cookie? If there are any kinds of mistakes in the above code, please enlighten me.

Any other ideas would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-12-30 06:17:42

最可能的问题是,您似乎认为方法中的最终赋值 (method = theMethod) 在 loadHttp302Request 之外有任何影响。 (编辑:原始代码有此语句,但OP后来更改了它)

它没有。

Java 没有引用调用语义,因此赋值没有实际效果。如果您想保留响应(最重要的是 cookie)以供下次调用,则需要返回 theMethod 并在下次调用时使用它。像这样的东西:

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

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

It doesn't.

Java doesn't have call-by-reference semantics, so that assignment has no net effect. If you want to retain the response (most importantly, the cookie) for a next invocation, you need to return theMethod and use that the next time. Something like:

private HttpMethod loadHttp302Request(HttpMethod method, HttpClient client, int status, String urlString) throws HttpException, IOException {
    // code as before
    return theMethod;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文