简体   繁体   English

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

[英]Maintaining Session using HTTPClient on a Rails Application

I have an applet that talks with a Rails Application. 我有一个与Rails应用程序对话的小程序。 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. 我使用apache的HTTPClient发送请求,但rails应用程序无法将请求识别为用户会话的一部分。

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: 这是我用来构建请求的代码,我将session_id变量和HTTP_COOKIE变量作为applet参数传递:

        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 懒散型

I solved this with the following code: 我用以下代码解决了这个问题:

String session_id_str = "6a7eec105k231g51bjd0655e638034f3"; 字符串session_id_str =“ 6a7eec105k231g51bjd0655e638034f3”;

Cookie sessionID = new Cookie("127.0.0.1:3001", "_chatter_session", session_id_str, "/", null, false); Cookie会话ID =新Cookie(“ 127.0.0.1:3001”,“ _ chatter_session”,session_id_str,“ /”,null,false);

HttpState initialState = new HttpState(); HttpState initialState =新的HttpState();

initialState.addCookie(sessionID); initialState.addCookie(sessionID);

client.setState(initialState); client.setState(initialState);

The two problems were: 两个问题是:

1) Not putting the port number in the url. 1)不将端口号放在URL中。

2) Specifying the wrong session id. 2)指定错误的会话ID。 In this example the application is called chatter so the session_id is _chatter_session. 在此示例中,该应用程序称为chatter,因此session_id为_chatter_session。 I add the session id to the applet as a parameter. 我将会话ID作为参数添加到applet中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM