Get URL properties - Java Network

Java examples for Network:URL Download

Description

Get URL properties

Demo Code


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class UrlClass {

    public static void main(String[] args) throws IOException {
        new UrlClass().example4();
    }//from ww w.ja  va  2 s. com

    private void example4() throws UnsupportedEncodingException {
        System.out.println(URLEncoder.encode("String with spaces", "UTF-8"));
        System.out.println(URLEncoder.encode("English", "UTF-8"));
        System.out.println(URLEncoder.encode("Special chars: &%*", "UTF-8"));
    }

    private void example3() throws MalformedURLException {
        URL uri = new URL("http://docs.oracle.com/javase/tutorial/index.html?name=networking#DOWNLOADING");
        System.out.println("getProtocol:" + uri.getProtocol());
        System.out.println("getAuthority:" + uri.getAuthority());
        System.out.println("getHost:" + uri.getHost());
        System.out.println("getPort:" + uri.getPort());
        System.out.println("getPath:" + uri.getPath());
        System.out.println("getQuery:" + uri.getQuery());
        System.out.println("getFile:" + uri.getFile());
        System.out.println("getRef:" + uri.getRef());

    }

    private void examle1() throws IOException {

        URL uri = new URL(
                "https://www.google.co.kr/images/branding/product/ico/googleg_lodp.ico");
        Object object = uri.getContent();

    }

    private void example2() throws IOException {
        URL uri = new URL("https://www.google.co.kr");
        BufferedReader theHtml = new BufferedReader(new InputStreamReader(
                uri.openStream()));
        String thisLine = null;
        while ((thisLine = theHtml.readLine()) != null)
            System.out.println(thisLine);

        theHtml.close();
    }

}

Related Tutorials