Example usage for java.net URL URL

List of usage examples for java.net URL URL

Introduction

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

Prototype

public URL(String spec) throws MalformedURLException 

Source Link

Document

Creates a URL object from the String representation.

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.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 w ww.j a  va  2 s  .  c om*/
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String urlString = "http://google.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();//from  w ww . j  ava 2 s. co  m
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Properties systemSettings = System.getProperties();
    systemSettings.put("proxySet", "true");
    systemSettings.put("http.proxyHost", "proxy.mycompany.local");
    systemSettings.put("http.proxyPort", "80");

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    BASE64Encoder encoder = new BASE64Encoder();
    String encodedUserPwd = encoder.encode("domain\\username:password".getBytes());
    con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
}

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  a  2 s  . 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: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  .  j  a  va2s.c o m
    }
}

From source file:Main.java

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

    SecurityManager sm = new SecurityManager();
    System.setSecurityManager(sm);
    URL codebase = new URL("http://java.sun.com/");

    //codebase = new File("c:\\java\\").toURI().toURL();
    //codebase = new File(System.getProperty("user.home")).toURI().toURL();

    CodeSource cs = new CodeSource(codebase, (Certificate[]) null);

    PermissionCollection pcoll = Policy.getPolicy().getPermissions(cs);

    Enumeration e = pcoll.elements();
    for (; e.hasMoreElements();) {
        Permission p = (Permission) e.nextElement();
    }/*w  ww.j  av a 2 s  .  c o  m*/
}

From source file:Main.java

public static void main(String s[]) throws Exception {
    try {/*from   w  w  w . ja  va 2 s . c  om*/
        Properties systemSettings = System.getProperties();
        systemSettings.put("proxySet", "true");
        systemSettings.put("http.proxyHost", "proxy.mycompany.local");
        systemSettings.put("http.proxyPort", "80");

        URL u = new URL("http://www.java.com");
        HttpURLConnection con = (HttpURLConnection) u.openConnection();
        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        String encodedUserPwd = encoder.encode("domain\\username:password".getBytes());
        con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
        con.setRequestMethod("HEAD");
        System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
        System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(false);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL urlImage1 = new URL("http://www.java2s.com/style/download.png");

    final Image fgImage = ImageIO.read(urlImage1);
    int w = fgImage.getWidth(null);
    int h = fgImage.getHeight(null);
    final BufferedImage bgImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    final BufferedImage finalImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = finalImage.createGraphics();
    g.drawImage(bgImage, 0, 0, null);/*  w ww  .java 2  s  .c  o m*/
    g.drawImage(fgImage, 0, 0, null);
    g.dispose();

    Runnable r = new Runnable() {
        @Override
        public void run() {
            JPanel gui = new JPanel(new GridLayout(1, 0, 5, 5));

            gui.add(new JLabel(new ImageIcon(bgImage)));
            gui.add(new JLabel(new ImageIcon(fgImage)));
            gui.add(new JLabel(new ImageIcon(finalImage)));

            JOptionPane.showMessageDialog(null, gui);
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    Image chunk = readFragment(url.openStream(), new Rectangle(15, 15, 30, 25));
    JOptionPane.showMessageDialog(null, new ImageIcon(chunk), "Duke", JOptionPane.INFORMATION_MESSAGE);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    URL url = new URL("http://www.java2s.com/style/download.png");
    Image image = ImageIO.read(url);

    ImagePanel ip = new ImagePanel(new GridLayout(4, 4, 20, 20));
    ip.setPreferredSize(new Dimension(640, 480));
    f.setContentPane(ip);//  w w  w .ja v  a  2s  . co m
    f.pack();
    f.setVisible(true);

    ip.setImage(new ImageIcon(image));
}