You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Frédérick Permantier <fr...@helpcorner.be> on 2006/09/29 23:14:04 UTC
Can't get HttpClient to work with a special webpage
Hi,
I just started to use HttpClient. I tested it on two webpages, I made it work on a
simple webpage (to loggin into SquirrelMail). But I can't make it work on this page :
http://www.edpnet.be/traffic2.aspx?R=1
I would like to know if there's something special to do about that webpage.
He is the code I used based upon a demo code (I removed my username and password from
the code, but if you want to help you can contact me by email...) :
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.Cookie;
public class Quota {
static final String LOGON_SITE = "www.edpnet.be";
static final int LOGON_PORT = 80;
public Quota() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE,LOGON_PORT,"http");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod authget = new GetMethod("/traffic2.aspx?R=1");
client.executeMethod(authget);
System.out.println("Login form get: " + authget.getStatusLine().toString());
// release any connection resources used by the method
authget.releaseConnection();
// See if we got any cookies
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] initcookies =
cookiespec.match(LOGON_SITE,LOGON_PORT,"/",false,client.getState().getCookies());
System.out.println("Initial set of cookies:");
if (initcookies.length == 0) {
System.out.println("None");
}
else {
for (int i = 0; i < initcookies.length; i++) {
System.out.println("- " + initcookies[i].toString());
}
}
PostMethod authpost = new PostMethod("/traffic2.aspx?R=1");
// Prepare login parameters
NameValuePair userid = new NameValuePair("tbUserName","******");
NameValuePair password = new NameValuePair("tbPassword","******");
authpost.setRequestBody(new NameValuePair[] {userid, password});
client.executeMethod(authpost);
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
// See if we got any cookies
// The only way of telling whether logon succeeded is
// by finding a session cookie
Cookie[] logoncookies =
cookiespec.match(LOGON_SITE,LOGON_PORT,"/",false,client.getState().getCookies());
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
System.out.println("None");
}
else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies[i].toString());
}
}
int statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
System.exit(1);
}
}
GetMethod getmail = new GetMethod("/traffic2_details.aspx");
client.executeMethod(getmail);
System.out.println(getmail.getResponseBodyAsString());
}
}
Thanks for your help !
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
Re: Can't get HttpClient to work with a special webpage
Posted by Roland Weber <ht...@dubioso.net>.
Hello Frederick,
> I just started to use HttpClient. I tested it on two webpages, I made it work on a
> simple webpage (to loggin into SquirrelMail). But I can't make it work on this page :
> http://www.edpnet.be/traffic2.aspx?R=1
>
> I would like to know if there's something special to do about that webpage.
Please have a look at our Client HTTP Programming Primer:
http://wiki.apache.org/jakarta-httpclient/ForAbsoluteBeginners
It is most likely that your problem is addressed there.
cheers,
Roland
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org