Results 1 to 8 of 8
  1. #1
    zuzacat is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Passing session ID over HttpURLConnection

    Hi,

    I have a jsp page and a class. I am creating a new object from the class in the jsp and in the class I have HttpURLConnection, the url given is authorization required it mean it checks for the session.

    What I want to do is I want to pass the session in jsp to HttpURLConnection.

    Here is my jsp:

    Java Code:
    ExportToPDF pdf = new ExportToPDF();
    pdf.setUrlString("http://www.javaprogrammingforums.com");
    pdf.createPDF(request, response, session);
    and here is my method in my class:

    Java Code:
    public void createPDF(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, ParserConfigurationException, 
    			SAXException, DocumentException {
    		
    		String sessionId = session.getId();
    		URL url	= new URL(this.getUrlString());
    		
    		HttpURLConnection urlConn = null;
    		urlConn = (HttpURLConnection) url.openConnection();
    		
    		urlConn.setRequestMethod("POST");
                    HttpURLConnection.setFollowRedirects(true);
                    urlConn.setDoOutput(true);
                    urlConn.setDoInput(true);
                    urlConn.setUseCaches(false);
                    urlConn.setAllowUserInteraction(false);
                    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    urlConn.setRequestProperty("Content-Language", "en-US");
                    urlConn.setRequestProperty("Cookie", sessionId);
    		
    		InputStream byteStream 	= urlConn.getInputStream();
    		
    		OutputStream os = response.getOutputStream();

    Can you help me what am I missing here ?
    Thank you.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    I might be being a bit confused here, but I presume that website you are trying to connect to is not the current website (server) you are running this code from?
    If so then why are you sending the session id for your current session on your server?

  4. #4
    zuzacat is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,
    Thank you for your response.

    Yes the URL I am trying connect is in the same server. I will try to explain what I am trying to do.

    The page which I want to connect is authorized with session value. I want to connect that page to create a PDF version of that page.

    So how can I achieve this ?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    So you've got an html to pdf converter, and you want to turn a page on your website into a PDF.
    OK.
    That Cookie parameter I believe should have "<parameter_name>="+sessionId.
    Now, I don't know what the parameter name for the session is is, so you might need to look in the cookie produced by your site and see.

  6. #6
    zuzacat is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,

    So the way I am on is correct isnt it ?
    There is only JSESSIONID which I need.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    I think so.
    This should set the cookie:
    Java Code:
    urlConn.setRequestProperty("Cookie", <correct string here>);
    Try it.

  8. #8
    zuzacat is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    I got it working. I forgot to include JSESSIONID in value.

    I changed this:
    urlConn.setRequestProperty("Cookie", "sessionid");
    with this:
    urlConn.setRequestProperty("Cookie", "JSESSIONID=sessionid");

    Thank you Tolls :)

Similar Threads

  1. C# HttpURLConnection to java
    By jmorr212 in forum Networking
    Replies: 0
    Last Post: 02-24-2011, 10:29 AM
  2. passing a db connection across jsps throughout a session
    By Tirka in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-18-2009, 01:32 PM
  3. Replies: 21
    Last Post: 05-21-2009, 01:53 PM
  4. Replies: 2
    Last Post: 12-23-2008, 06:35 PM
  5. Replies: 4
    Last Post: 10-15-2008, 01:59 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
  •