I am up with the HTTPClient module,but was stuck with a few things.
1.My code is supposed to login to a site and get the data,but here what i see is a html source which is nothing but a login page.
2.How can i click on a particular link on a page.
Here's the code that i was trying to use:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.HttpAuthRealm;
import org.apache.commons.httpclient.methods.*;
public class SecureGetPageExample {
public static void main(
String[] args ) {
/*if( args.length < 3 ) {
System.out.println( "Usage:
java GetPageExample URL username password" );
System.exit( 0 );
}*/
String url = "http://www.gmail.com";
String username = "";
String password = "";
try {
HttpClient client = new HttpClient();
client.getState().setCredentials(
new HttpAuthRealm(password, username),
new UsernamePasswordCredentials( username, password ) );
GetMethod method = new GetMethod( url );
method.setFollowRedirects( true );
// Execute the GET method
int statusCode = client.executeMethod( method );
if( statusCode != -1 ) {
String contents = method.getResponseBodyAsString();
method.releaseConnection();
System.out.println( contents );
}
}
catch( Exception e ) {
e.printStackTrace();
}
}
}