d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Saving Cookies
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Sep 25 2012 11:14am
I am trying to write a program that updates inventory based on a website. However, the website only gives item information if you are logged into it.

I know how to submit my username and password, but i dont know how to keep that information valid when i do the item searches.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 25 2012 02:49pm
Just use a virtual browser like python's mechanize. it handles cookies and all that for you.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 26 2012 01:00pm
what language are you programming in? if Java, now is a good time to learn apache HttpClient
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Sep 26 2012 02:20pm
Quote (carteblanche @ Sep 25 2012 01:49pm)
Just use a virtual browser like python's mechanize. it handles cookies and all that for you.


I would like to use java because this is a very small portion of a much bigger program.

Quote (irimi @ Sep 26 2012 12:00pm)
what language are you programming in?  if Java, now is a good time to learn apache HttpClient


I have read a bit about it, but it seemed a bit robust for what i needed. Is there a simple answer to this? All i need to do is retain my login information while i navigate through a site.

Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 27 2012 12:21pm
if this is just for something manual, you could always login on Firefox, then use an addon like Export Cookies:
https://addons.mozilla.org/en-US/firefox/addon/export-cookies/

take that file and use it as your basis for authentication (with curl, for example)
Member
Posts: 2,579
Joined: Jun 1 2012
Gold: 1,524.00
Sep 28 2012 02:35pm
You need to add a Cookie (in the Servlet class) to the HttpServletResponse when you sent it back to the client. Persist it from there.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 5 2012 01:45am
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
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll