Example usage for java.net CookieManager CookieManager

List of usage examples for java.net CookieManager CookieManager

Introduction

In this page you can find the example usage for java.net CookieManager CookieManager.

Prototype

public CookieManager() 

Source Link

Document

Create a new cookie manager.

Usage

From source file:FetchCookie.java

public static void main(String args[]) throws Exception {
    String urlString = "http://java.sun.com";
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();/*w ww.  j a v a2s.  c  o  m*/
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

From source file:ListAllCookies.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("usage: java ListAllCookies url");
        return;/* w  ww .  j  a va 2  s  .c o  m*/
    }

    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);

    new URL(args[0]).openConnection().getContent();

    List<HttpCookie> cookies = cm.getCookieStore().getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println("Name = " + cookie.getName());
        System.out.println("Value = " + cookie.getValue());
        System.out.println("Lifetime (seconds) = " + cookie.getMaxAge());
        System.out.println("Path = " + cookie.getPath());
        System.out.println();
    }
}

From source file:HttpCilentExample.HttpCilentExample.java

public static void main(String[] args) throws Exception {

    String url = "https://accounts.google.com/ServiceLoginAuth";
    String gmail = "https://mail.google.com/mail/";

    // make sure cookies is turn on
    CookieHandler.setDefault(new CookieManager());

    HttpCilentExample http = new HttpCilentExample();

    String page = http.GetPageContent(url);

    List<NameValuePair> postParams = http.getFormParams(page, "username", "password");

    http.sendPost(url, postParams);//www.ja v  a  2 s. c o m

    String result = http.GetPageContent(gmail);
    System.out.println(result);

    System.out.println("Done");
}

From source file:Main.java

public static CookieManager cookieManager() {
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    return cookieManager;
}

From source file:Main.java

/**
 * Creates a new CookieManager instance.
 */
public static CookieHandler newCookieManager() {
    return new CookieManager();
}

From source file:Main.java

/**
 * @see http://blogs.sun.com/CoreJavaTechTips/entry/cookie_handling_in_java_se
 *//*  ww w.j a v a 2  s . c  o  m*/
public static void enableCookieMgmt() {
    CookieManager manager = new CookieManager();
    manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(manager);
}

From source file:edgeserver.Publicacao.java

public void publica(String urlLogin, String urlInsertDado) throws Exception {
    // make sure cookies is turn on
    CookieHandler.setDefault(new CookieManager());

    Date datapublicacao = new Date();

    HTTPClient http = new HTTPClient();

    List<NameValuePair> postp = new ArrayList<>();
    postp.add(new BasicNameValuePair("login", "huberto"));
    postp.add(new BasicNameValuePair("password", "99766330"));

    http.sendPost(urlLogin, postp);//from  ww  w.  ja  v  a2  s . co  m

    List<NameValuePair> GatewayParams = new ArrayList<>();
    GatewayParams.add(new BasicNameValuePair("publicacao_servidorborda", Integer.toString(this.servidorborda)));
    GatewayParams.add(new BasicNameValuePair("publicacao_sensor", Integer.toString(this.sensor)));
    GatewayParams.add(new BasicNameValuePair("publicacao_datacoleta", this.datacoleta.toString()));
    GatewayParams.add(new BasicNameValuePair("publicacao_datapublicacao",
            new Timestamp(datapublicacao.getTime()).toString()));
    GatewayParams.add(new BasicNameValuePair("publicacao_valorcoletado", Float.toString(this.valorcoletado)));

    String result = http.GetPageContent(urlInsertDado, GatewayParams);
}

From source file:info.aamulumi.sharedshopping.network.RequestSender.java

/**
 * Constructor - Init the cookie manager
 */
public RequestSender() {
    mCookieManager = new CookieManager();
}

From source file:horriblev3.Cloudflare.java

public List<HttpCookie> scrape() {
    if (strUrl == null) {
        System.out.println("URL == NULL");
        return null;
    }//from  w  w  w  . j  a  v  a 2 s  .c om
    try {
        CookieManager manager = new CookieManager();
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(manager);

        URL url = new URL(strUrl);
        HttpURLConnection con;
        con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("User-Agent", USERAGENT);
        InputStream _is;
        if (con.getResponseCode() == 200) {
            return retrieveCookies(manager);
        } else {
            _is = con.getErrorStream();
            StringBuilder result = new StringBuilder();
            try (BufferedReader rd = new BufferedReader(new InputStreamReader(_is))) {
                String line;
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
            }
            String source = result.toString();

            //extract challenge
            String challenge = regex(source, "name=\"jschl_vc\" value=\"(\\w+)\"");
            String challenge_pass = regex(source, "name=\"pass\" value=\"(.+?)\"");

            //prepare
            String builder = regex(source,
                    "setTimeout\\(function\\(\\)\\{\\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\\r?[\\s\\S]+?a\\.value =.+?)\\r?\\s+';");
            builder = builder.replaceFirst("\\s{3,}[a-z](?: = ).+form'\\);\\s+;", "").replaceFirst(
                    "a\\.value = parseInt\\(.+?\\).+", regex(builder, "a\\.value = (parseInt\\(.+?\\)).+"));

            //Execute&solve
            System.out.println(builder);
            long solved = Long.parseLong(solveJS2(builder));
            solved += url.getHost().length();
            System.out.println("SOLVED: " + solved);

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }

            URI tmp = UrlToUri(url);
            List<NameValuePair> qparams = new ArrayList<>();
            qparams.add(new BasicNameValuePair("jschl_vc", challenge));
            qparams.add(new BasicNameValuePair("jschl_answer", String.valueOf(solved)));
            qparams.add(new BasicNameValuePair("pass", challenge_pass));
            URIBuilder uriBuilder = new URIBuilder().setScheme(tmp.getScheme()).setPath("/cdn-cgi/l/chk_jschl")
                    .setHost(tmp.getHost()).setParameters(qparams);

            HttpURLConnection cookie_req = (HttpURLConnection) new URL(uriBuilder.toString()).openConnection();
            cookie_req.setRequestProperty("Referer", url.toString());
            cookie_req.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:48.0) Gecko/20100101 Firefox/48.0");
            HttpURLConnection.setFollowRedirects(false);
            cookie_req.connect();

            System.out.println("ResponseCode: " + cookie_req.getResponseCode());
            if (cookie_req.getResponseCode() == 200) {
                return retrieveCookies(manager);
            } else {
                System.out.println("Something went wrong!");
                return null;
            }
        }
    } catch (IOException e1) {
        System.out.println(e1.getMessage());
        return null;
    }
}

From source file:net.kevxu.muzei.interfacelift.InterfaceliftMacdropsClient.java

public InterfaceliftMacdropsClient(Context context, String license) {
    mContext = context;/*  ww w .  j  ava2  s. c  o m*/
    mLicense = license;

    // Enable Cookie for HttpURLConnection
    CookieManager cookieManager = new CookieManager();
    CookieHandler.setDefault(cookieManager);
}