jasjisdo
5/6/2016 - 11:19 AM

[JAVA] How to get the JSession id from Http connection (with Jsoup)

[JAVA] How to get the JSession id from Http connection (with Jsoup)


public class JsoupHttpSesssionIdReceiver {
    public static void main(String... args) {
        // open connection with connection factory 
        Connection connection = ConnectionFactory.getDefault("http://www30.zippyshare.com/v/4Bd0xZh3/file.html");
        
        Connection.Response response = null;
        // execute connection 
        try {
            response = connection.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // get cookie by name from response
        System.out.println("JSESSIONID=" + response.cookie("JSESSIONID"));
    }
}
public class ConnectionFactory {

    protected static String userAgent = "Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/43.0";
    protected static int timeout = 15000;

    public static Connection getDefault(String url){
        return Jsoup.connect(url)
                .userAgent(userAgent)
                .timeout(timeout);
    }
}