Quote (xandumx @ Oct 4 2012 03:24pm)
You commented on my thread several days ago (
http://forums.d2jsp.org/topic.php?t=64699939&f=124), but I am having a very hard time figuring out a solution. I have little time to work on this project, but I would really like to incorporate this functionality into a program I am writing.
Would you be able to write a Java function (with some comments so I can understand it), that: 1) logs into a site, 2) saves all the cookies, 3) Sends a POST request, 4) displays the html from the POST.I would give you a real example to work because I have having trouble understanding abstract examples online.
How much would this cost in fg?Here's a simple HttpClient driver that's capable of visiting a URL, dealing with a security redirect, logging into the site, and grabbing the content. Notice the complete lack of cookie handling code, since HttpClient does it pretty much automatically. There's some shenanigans with the SSL code here -- assuming your site uses HTTPS, this will allow you to talk SSL with them without having to do server certificate verification.
Your use case may be simpler -- if you already know the URL of the login form, you can probably drop the first GET and just start with the POST. But basically, if you're using the same HttpClient instance, cookies will automatically get preserved.
Code
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
public class HttpClientDriver {
private static SSLSocketFactory createTrustAllSocketFactory() throws Exception {
return new SSLSocketFactory(new TrustStrategy() {
public boolean isTrusted(
final X509Certificate[] chain, String authType) throws CertificateException {
// Oh, I am easy...
return true;
}
}, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
public static void main(String[] args) throws Exception {
run("https://www.somewebsite.com/", "https://www.somewebsite.com/form", "uUsername", "uPassword",
"yourUserNameHere", "yourPasswordHere");
}
public static void run(String startUrl, String loginFormUrl, String usernameField, String passwordField,
String username, String password) throws Exception {
Scheme httpsScheme = new Scheme("https", 443, createTrustAllSocketFactory());
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(httpsScheme);
HttpParams params = new BasicHttpParams();
ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
try {
// initial GET, following the redirect to the login form
HttpGet httpget = new HttpGet(startUrl);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
// ok, we're at the login form now; fill it in and POST the login params
HttpPost httpost = new HttpPost(loginFormUrl);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(usernameField, username));
nvps.add(new BasicNameValuePair(passwordField, password));
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
// set to follow all ensuing redirects
httpclient.setRedirectStrategy(new LaxRedirectStrategy());
// run the POST
response = httpclient.execute(httpost);
// grab the response
EntityUtils.consume(response.getEntity());
entity = response.getEntity();
String content = EntityUtils.toString(entity);
System.out.println("Login form get: " + response.getStatusLine());
System.out.println("Content: " + content);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
This post was edited by irimi on Oct 5 2012 01:48am