commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@bluewin.ch>
Subject Re: [HttpClient] How to use the cookie JSessionID for Applet/J2EE Connections.
Date Wed, 17 Mar 2004 19:38:51 GMT
Laurent,

Try using the session cookie instead:

String jsessionid = getParameter(SESSIONID_PARAMETER);
HttpClient agent = new HttpClient();
Cookie sessionCookie = new Cookie(".mycompany.com", "jsessionid",
  jsessionid, "/", null, false);
agent.getState().addCookie(sessionCookie);  
GetMethod httpget = new GetMethod(
  "http://mycompany.com/protected/whatever.url");
try {
 agent.executeMethod(httpget);
 System.out.println(httpget.getStatusLine());
}
finally {
  httpget.releaseConnection();
}

Make sure that you get the domain and path attributes of the session
cookie right, otherwise the cookie will not be sent with the request

Oleg


On Wed, 2004-03-17 at 14:17, Laurent ForĂȘt wrote:
> Hello,
> 
> According to 2 threads in jguru.com website (33978, 85187), I try to use httpclient for
connection 
> between applet and j2ee server.
> For this, I want to reuse the opened session by the browser : using javascript I give
the JSESSIONID 
> cookie value to the applet, and I try to use this cookie value with httpclient, without
success.
> Does anyone can help me !
> 
> Thanks, Laurent F.
> 
> 
> The code applet is :
> 
> public class TestHttpClientApplet extends JApplet {
> 
>    private static String SESSIONID_PARAMETER = "jsessionid";
>    private static String URL_PARAMETER = "serverurl";
> 
>    private MyHttpClient httpClient_;
> 
>    public TestHttpClientApplet() {
>      super();
>    }
> 
>    public void init() {
>      String jsessionid = getParameter(SESSIONID_PARAMETER);
>      String url = getParameter(URL_PARAMETER);
>      httpClient_ = new MyHttpClient(jsessionid);
>      JTextArea text = new JTextArea(100, 100);
>      setContentPane(new JScrollPane(text));
>      text.setText(new String(httpClient_.get(url)));
>    }
> }
> 
> 
> The MyHttpClient code looks like this:
> 
> public class MyHttpClient {
> 
>    private HttpClient httpClient_ = new HttpClient();
> 
>    private String sessionId_;
>    private final static String SESSION_ID = "JSESSIONID";
> 
>    public HttpClipackClient(String sessionId) {
>      sessionId_ = sessionId;
>    }
> 
>    public byte[] get(String url) throws MyException  {
> //    GetMethod get = new GetMethod(url+";jsessionid="+sessionId_);
>      GetMethod get = new GetMethod(url);
>      get.setRequestHeader(SESSION_ID, sessionId_);
>      try {
>        statusCode = httpClient_.executeMethod(method);
>      } catch (HttpRecoverableException hrex) {
>        throw MyException(hrex);
>      } catch (IOException ioex) {
>        throw MyException(ioex);
>      }
> 
>      if (statusCode == -1) {
>        throw new MyException("Can not post "+method.getName());
>      }
>      byte[] response = method.getResponseBody();
>      method.releaseConnection();
>      return response;
> 
>    }
> }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message