Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Http post to login to forum

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Http post to login to forum

    Well, I need to make a java project that uses a urlConnection so I picked logging into a forum. I need to send an http post request to a site and then test to see if I'm logged in. This is a snippet of the code. This piece should login by sending the post data to Sign In but when it reads the html of the next page, it just prints out the login page so either I'm doing something wrong or it needs to log/send the cookies that the site sends. Any input would be greatly appreciated. Thanks.

    	final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
        final String LOGIN_ACTION_NAME = "submit";
        final String LOGIN_USER_NAME_PARAMETER_NAME = "username";
        final String LOGIN_PASSWORD_PARAMETER_NAME = "password";
     
        final String LOGIN_USER_NAME = "myusername";
        final String LOGIN_PASSWORD = "mypassword";
     
        String encodedLoginUserName = URLEncoder.encode(LOGIN_USER_NAME, "UTF-8");
        String encodedLoginPassword = URLEncoder.encode(LOGIN_PASSWORD, "UTF-8");
    //referer=http%3A%2F%2Fforum.tip.it%2Findex&username=myusername&password=mypassword&rememberMe=1
        String content = "referer=http%3A%2F%2Fforum.tip.it%2Findex&" + LOGIN_USER_NAME_PARAMETER_NAME +"="
        + encodedLoginUserName + "&" + LOGIN_PASSWORD_PARAMETER_NAME + "=" + encodedLoginPassword + "&rememberMe=1";
    	 System.out.println(content);
    	// Send data
    URL url = new URL("http://forum.tip.it/index.php?app=core&module=global&section=login");
    HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
     
    httpcon.setRequestMethod("POST");
    httpcon.setDoOutput(true);
    httpcon.setDoInput(true);
    DataOutputStream wr = new DataOutputStream(httpcon.getOutputStream());
    wr.writeBytes(content);
    wr.flush();
    wr.close();
    //write cookies?
    	         // Get the response
    	         BufferedReader rd = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
    	         String line;
    	         while ((line = rd.readLine()) != null) {
    	             System.out.println(line);
             }
    	         wr.close();
    	         rd.close();
    				httpcon.disconnect();
     
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Http post to login to forum

    You most likely need to add support for cookies, see Cookie Support. I'd also recommend reading the 3rd party rules carefully, just in case automation of this sort of things is frowned upon
    Last edited by copeg; February 14th, 2010 at 10:05 AM.

Similar Threads

  1. Sending XML over HTTP to Another Application
    By darasgar in forum Java Servlet
    Replies: 2
    Last Post: July 14th, 2010, 01:56 PM
  2. simple login web service
    By mr_aliagha in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 5th, 2010, 02:49 PM
  3. Post variable set to non-null vlaues
    By ss7 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2009, 11:04 AM
  4. Replies: 6
    Last Post: October 20th, 2009, 06:33 AM
  5. cant get rid of http connection
    By kartik in forum Java Networking
    Replies: 1
    Last Post: July 21st, 2009, 03:09 AM