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[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_NOT_AUTHORITATIVE);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.java2s.com");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String baseURLStr = "http://www.ietf.org/rfc/rfc3986.txt";
    String relativeURLStr = "rfc2732.txt";
    URL baseURL = new URL(baseURLStr);
    URL resolvedRelativeURL = new URL(baseURL, relativeURLStr);
    System.out.println("Base URL:" + baseURL);
    System.out.println("Relative URL  String:" + relativeURLStr);
    System.out.println("Resolved Relative URL:" + resolvedRelativeURL);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.com/index.html");
    URLConnection connection = url.openConnection();

    Map responseMap = connection.getHeaderFields();
    for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) {
        String key = (String) iterator.next();
        System.out.println(key + " = ");

        List values = (List) responseMap.get(key);
        for (int i = 0; i < values.size(); i++) {
            Object o = values.get(i);
            System.out.println(o + ", ");
        }/*from ww w  . j a v a 2 s  .  c om*/
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:8080/postedresults.jsp");
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);/*  ww w . j ava 2  s  .c  o m*/
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    String content = "CONTENT=HELLO JSP !&ONEMORECONTENT =HELLO POST !";

    out.writeBytes(content);
    out.flush();
    out.close();

    DataInputStream in = new DataInputStream(conn.getInputStream());
    String str;
    while (null != ((str = in.readUTF()))) {
        System.out.println(str);
    }
    in.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);

    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setInstanceFollowRedirects(false);

    conn.connect();/*from w  w w . j  ava  2 s .  c  o  m*/
}

From source file:URLGet.java

public static void main(String[] args) {
    BufferedReader in = null;//from   w w w. ja  v  a 2  s  .c o m
    if (args.length == 1) {
        try {
            URL url = new URL(args[0]);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = null;
            while ((line = in.readLine()) != null)
                System.out.println(line);
        } catch (MalformedURLException ex) {
            System.err.println(ex);
        } catch (FileNotFoundException ex) {
            System.err.println("Failed to open stream to URL: " + ex);
        } catch (IOException ex) {
            System.err.println("Error reading URL content: " + ex);
        }
        if (in != null)
            try {
                in.close();
            } catch (IOException ex) {
            }
    } else
        System.err.println("Usage: URLGet URL");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL u = new URL("http://www.google.com");
    InputStream in = u.openStream();
    MessageDigest sha = MessageDigest.getInstance("SHA");
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        sha.update(data, 0, bytesRead);//from  ww w. jav a  2  s  .c  o m
    }
    byte[] result = sha.digest();
    System.out.println(Arrays.toString(result));
    System.out.println(new BigInteger(result));
}