Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I'm trying to login on an internet Page. I've been googeling and trying to find the right way to achieve this. What i got this far is:

I'm using the Apache package (HttpClient, Code, Logging).

import java.io.*;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.Header;

public class TestCode {

	static final String loginSite = "****";
	static final String checkSite = "****"; 
	
	public static void main(String[] args) throws Exception{

		HttpClient client = new HttpClient();

              cient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		PostMethod post = new PostMethod(loginSite);

		post.setRequestHeader(new Header("User-Agent", "****"));
		
		post.addParameter("name","****");
		post.addParameter("password","****");
		post.addParameter("button_name","****"); //<--Press Button?
		
		client.executeMethod(post);

		
		BufferedReader in = new BufferedReader(
				new InputStreamReader(
						post.getResponseBodyAsStream()));

		String decodedString;
		
		while ((decodedString = in.readLine()) != null) {
			System.out.println(decodedString);
		}
		in.close();
				
		post.releaseConnection();
				 
	}
	
}


This is the Internet Packe i'm trying to log in:

<<pre lang="xml">form method="post" name="post_name" action="TMP.php">
    <table cellpadding="1" cellspacing="1" id="login_form">
        <tbody>
            <tr class="top">
                <th>Enter Name:</th>
                <td>
                    <input class="text" type="text" name="name" value="Login_Name_Here" /> <span class="error"> </span>
                </td>
            </tr>
            <tr class="btm">
                <th>Enter Password:</th>
                <td>
                    <input class="text" type="password" name="password" value=""
                    maxlength="20" /> <span class="error"> </span>
                </td>
            </tr>
        </tbody>
    </table>
    <p class="btn">
                <input type="image" value="login" name="button_name" onclick="xy();" id="btn_login" class="dynamic_img" src="img/x.gif" alt="Login-Button" />
        <input type="hidden" name="w" value="" />
        <input type="hidden" name="login" value="99999999" />
    </p>
</form>



In the response i get, the password & name is fild in.

I'm not sure if i have to "press" the Button or not.

Also wenn i get the response i'm still on the login page (name & password ist fild in the under "value").

Usually when i login on my browser the i-net page sends me to an other page
(Just changed the page: TMP.php).

Anyone has a clue?

Thanks in advance.

----------------------------------------------------------------

Ok, it's me again,

right now i'm trying on an easier example. No login!
But still i don't get the PostMethod...

What works & and what i understand is the GetMethod:
http://hc.apache.org/httpclient-3.x/tutorial.html
-> I get the information of the internet page.

But what doesn't work still is the PostMethod:
http://hc.apache.org/httpclient-3.x/methods/post.html
-> Here I get the same information (like with GetMethod)

Has anybody a working PostMethod example on an excisting internet page??

Any help is appreciated
Posted
Updated 11-Jul-11 11:50am
v2
Comments
Joda-oda 30-Jun-11 11:17am    
Is there a way to see what the post-method sends?

1 solution

please elaborate your problem where it is ?
 
Share this answer
 
Comments
Joda-oda 29-Jun-11 8:36am    
The problem is, that i can't login on the internat page. When i read the response, i still get the httml code for the loginpage. The value of name and password is in the httml code, but i don't get the user specified data from the page. I'm not sure if i have to press the button. I allready achieved to login to the page with visual basic and a webbrowser, just by filling out the textboxes and invoking the button. But i don't know how i have to do this with java & the Apache pack.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900