Results 1 to 5 of 5
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default HttpURLConnection session

    I am trying to make a program which connects and logins to website using HttpURLConnection.
    For some reason it doesn't login. I think it's because it doesn't store the session.
    How can I make it so it stores the session?
    I am using this method to connect and login:
    Java Code:
    public void connect() {
            try {
                String param1 = "email";
                String param2 = "pass";
    
                String query = String.format("email=%s&pass=%s",
                        URLEncoder.encode(param1, "utf-8"),
                        URLEncoder.encode(param2, "utf-8"));
    
    
                URL url = new URL("http://website.com/emlogin.php");
                conn = (HttpURLConnection) url.openConnection();
    
    
                String cookies = "session_cookie";
                conn.setRequestProperty("Cookie", cookies);
    
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
    
                DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                out.writeBytes(query);
                out.flush();
    
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
    
                connected = true;
            } catch (MalformedURLException e) {
                connected = false;
                e.printStackTrace();
            } catch (IOException e) {
                connected = false;
                e.printStackTrace();
            }
        }

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: HttpURLConnection session

    I think I need to store the session as a cookie?
    How can I do that?

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: HttpURLConnection session

    upupupupupupup

  4. #4
    kkji is offline Member
    Join Date
    Aug 2011
    Posts
    54
    Rep Power
    0

    Default Re: HttpURLConnection session

    Just check this URL it may help
    Servlet Tutorial: Session Tracking

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: HttpURLConnection session

    Quote Originally Posted by kkji View Post
    Just check this URL it may help
    Servlet Tutorial: Session Tracking
    I'm not using servlets.

Similar Threads

  1. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 3
    Last Post: 07-06-2011, 04:36 PM
  2. POST request using HttpUrlConnection
    By turk in forum Networking
    Replies: 2
    Last Post: 07-06-2011, 04:29 PM
  3. Passing session ID over HttpURLConnection
    By zuzacat in forum New To Java
    Replies: 7
    Last Post: 06-21-2011, 02:02 PM
  4. C# HttpURLConnection to java
    By jmorr212 in forum Networking
    Replies: 0
    Last Post: 02-24-2011, 10:29 AM
  5. Replies: 21
    Last Post: 05-21-2009, 01:53 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
  •