在 Rails 应用程序上使用 HTTPClient 维护会话

发布于 2024-10-08 11:31:53 字数 737 浏览 2 评论 0原文

我有一个与 Rails 应用程序对话的小程序。我希望维护用户的会话,以便小程序的通信被识别为用户浏览会话的一部分。我使用 apache 的 HTTPClient 发送请求,但 Rails 应用程序无法将该请求识别为用户会话的一部分。

这是我用来构建请求的代码,我传入 session_id 变量和 HTTP_COOKIE 变量作为小程序参数:

        HttpClient client = new HttpClient();
        Cookie httpCookie = new Cookie("localhost", "HTTP_COOKIE", http_cookie, "/", null, false);
        Cookie sessionID = new Cookie("localhost", "session_id", session_id, "/", null, false);

        HttpState initialState = new HttpState();
        initialState.addCookie(httpCookie);
        initialState.addCookie(sessionID);
        client.setState(initialState);

        PostMethod post = new PostMethod("http://localhost:3001/vizs/add");

任何建议都会很棒!

懒惰型

I have an applet that talks with a Rails Application. I wish to maintain a user's session so that the applet's communication is recognized as part of a user's browsing session. I use apache's HTTPClient to send the request but the rails application does not recognize the request as part of the users session.

This is the code that I use to build the request, I pass in session_id variable and the HTTP_COOKIE variable as applet parameters:

        HttpClient client = new HttpClient();
        Cookie httpCookie = new Cookie("localhost", "HTTP_COOKIE", http_cookie, "/", null, false);
        Cookie sessionID = new Cookie("localhost", "session_id", session_id, "/", null, false);

        HttpState initialState = new HttpState();
        initialState.addCookie(httpCookie);
        initialState.addCookie(sessionID);
        client.setState(initialState);

        PostMethod post = new PostMethod("http://localhost:3001/vizs/add");

Any suggestions would be great!

slothishtype

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

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

发布评论

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

评论(1

苄①跕圉湢 2024-10-15 11:31:53

我用以下代码解决了这个问题:

字符串session_id_str =“6a7eec105k231g51bjd0655e638034f3”;

Cookie sessionID = new Cookie("127.0.0.1:3001", "_chatter_session", session_id_str, "/", null, false);

HttpState 初始状态 = new HttpState();

initialState.addCookie(sessionID);

client.setState(initialState);

这两个问题是:

1)没有将端口号放入 url 中。

2) 指定了错误的会话 ID。在此示例中,应用程序称为chatter,因此session_id 为_chatter_session。我将会话 ID 作为参数添加到小程序中。

I solved this with the following code:

String session_id_str = "6a7eec105k231g51bjd0655e638034f3";

Cookie sessionID = new Cookie("127.0.0.1:3001", "_chatter_session", session_id_str, "/", null, false);

HttpState initialState = new HttpState();

initialState.addCookie(sessionID);

client.setState(initialState);

The two problems were:

1) Not putting the port number in the url.

2) Specifying the wrong session id. In this example the application is called chatter so the session_id is _chatter_session. I add the session id to the applet as a parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文