Results 1 to 5 of 5
  1. #1
    sakshi.13agg is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Get Cookie object from HttpURLConnection object.

    Hello,

    I have created a HttpURLConnection with server url and thus to maintain session i need to store the cookies.
    To get the list of cookies I am using the following code-

    List cookieList = (List) connection.getHeaderFields().get("Set-Cookie");

    This cookieList contains cookies as string.
    Now to get session id from above cookie string we cannot hardcode JSESSIONID because not all web servers use JSESSIONID keyword to denote session id.
    Please help me in getting session id.

    Regards,
    Sakshi
    Last edited by sakshi.13agg; 08-27-2012 at 09:02 AM.

  2. #2
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Get Cookie object from HttpURLConnection object.

    Your assumption that the session information is stored in a cookie may be flawed since the session information may be stored in other ways such as URL rewrite or hidden fields. In any case if you know a cookie is being used you don't care what the cookie name is; you just return ALL the cookies sent by the server.

  3. #3
    sakshi.13agg is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Get Cookie object from HttpURLConnection object.

    Following is my scenario:

    I have a desktop client which communicates with web server.
    -I need to get sessionid from this desktop client.
    -I have a jsp page to which i need to send this session id as url parameter.
    -Launch this url in browser.
    -Inside this jsp i am reading the session id and creating a cookie in which i will set this id .
    -Now add this cookie to the response and redirect to some other url.
    This is done so that client server connection from browser will share desktop client server session.
    Please help me in achieving this.

  4. #4
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Get Cookie object from HttpURLConnection object.

    I see nothing in your protocol that stops you treating ALL the cookies sent by the server as the session id. Your desktop client will just pass ALL the cookies sent by the server to the browser back to the server.

    P.S. JSP are for display; you should use a Servlet for the control.

  5. #5
    farrukh is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Get Cookie object from HttpURLConnection object.

    I have a desktop client which communicates with web server.
    -I need to get sessionid from this desktop client.

    If you have a simple tomcat based server that uses the default authentication mechanism then you should be able to just use JSESSIONID. otherwise you atleast need to know the possible names for the session id. you can write a code like this

    private static final String[] SESS_ID_NAMES = {"JSESSIONID","ASPSESSIONID","SID","PHPSESSIONID", "NID"};

    private static String getSessionId(String cookie)
    {
    for(String sidname:SESS_ID_NAMES) {
    if (cookie.contains(sidname)) {
    int index =cookie.indexOf(sidname);
    return cookie.substring(index+sidkeyword.length()+1,cooki e.indexOf(";",index));
    }
    }
    return null;
    }

    public static void main(String []args)
    {

    HttpURLConnection urlcon = (HttpURLConnection)new URL("http://yoursite.com").openConnection();
    String sessionid =getSessionId(urlcon.getHeaderField("Set-Cookie"));
    }



    -I have a jsp page to which i need to send this session id as url parameter.
    -Launch this url in browser.
    -Inside this jsp i am reading the session id and creating a cookie in which i will set this id .

    You don't need to manually do it. You can simply hit your jsp page (or any page on your server) with sessionid like this
    http://yoursite.com/any_page;JSESSIONID=xxxxxx
    and it will establish a session between your client and server


    -Now add this cookie to the response and redirect to some other url.
    The server will automatically generate cookie and send it to your browser, your browser will store it. you can do a simple response.sendRedirect("newsite.com") in your server side code to redirect without worrying about the cookie.
    This is done so that client server connection from browser will share desktop client server session.
    Please help me in achieving this.
    Last edited by farrukh; 08-28-2012 at 01:15 AM.

Similar Threads

  1. Replies: 1
    Last Post: 01-09-2012, 05:46 PM
  2. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  4. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  5. Replies: 1
    Last Post: 07-22-2008, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •