Example usage for java.net CookieHandler setDefault

List of usage examples for java.net CookieHandler setDefault

Introduction

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

Prototype

public static synchronized void setDefault(CookieHandler cHandler) 

Source Link

Document

Sets (or unsets) the system-wide cookie handler.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);

    new URL("http://google.com").openConnection().getContent();
    CookieStore cookieStore = cm.getCookieStore();

    List<HttpCookie> cookies = cookieStore.get(new URI("http://google.com"));
    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 w w  w.  jav  a  2s  .c o m
    }
}

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;//from  w w  w  .ja  va2  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:Fetch5.java

  public static void main(String args[]) throws Exception {
  String urlString = "java.sun.com";
  CookieHandler.setDefault(new ListCookieHandler());
  URL url = new URL(urlString);
  URLConnection connection = url.openConnection();
  Object obj = connection.getContent();
  url = new URL(urlString);
  connection = url.openConnection();/*from  w  ww. j ava 2 s  . co m*/
  obj = connection.getContent();
}

From source file:WebClient.java

public static void main(String[] args) throws Exception {
        CookieStore store = new MyCookieStore();
        CookiePolicy policy = new MyCookiePolicy();
        CookieManager handler = new CookieManager(store, policy);
        CookieHandler.setDefault(handler);
        URL url = new URL("http://localhost:8080/cookieTest.jsp");
        URLConnection conn = url.openConnection();

        InputStream in = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String input;/*from  ww  w.  ja va2  s .  co  m*/
        while ((input = reader.readLine()) != null) {
            System.out.println(input);
        }
        reader.close();

    }

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);/*from w w w. j a va  2 s  .  c o m*/

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

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

From source file:Main.java

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

From source file:jmc.util.UtlFbComents.java

public static void getComentarios(Long idContenido, Long numComents) throws JMCException {

    File f = null;/*from  w w w .  j  av  a2  s . c o  m*/

    try {
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL("http://graph.facebook.com/comments?id="
                + Contenido.getContenido("Where cont_id = '" + idContenido + "'").get(0).getEnlaceURL()
                + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));
        String dirTempHtml = props.getProperty("archive_json") + "/";
        File fld = new File(dirTempHtml);

        boolean creado = fld.exists() ? true : fld.mkdir();

        if (creado) {
            String archFile = idContenido + ".json";
            String archivo = dirTempHtml + archFile;
            String linea = null;

            f = new File(archivo);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            while ((linea = br.readLine()) != null) {
                bw.append(linea);
            }
            bw.close();

            cn.disconnect();

            List<Fbcoment> lfb = Convertidor.getFbcomentObjects(idContenido, archivo);
            Fbcoment.actComents(idContenido, lfb);

        } else {
            cn.disconnect();
        }

    } catch (IOException e) {
        throw new JMCException(e);
    }
}

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);/*  w  ww.  j a va  2s . c o  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:gmusic.api.comm.HttpUrlConnector.java

public HttpUrlConnector() {
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

}

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

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

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