//Create URL can specify the requested URL protocol, but different JVM support protocols may not be the same (most support HTTP, file, HTTPS)
The //construct only determines that the protocol support in the string is not supported, and does not determine the correctness of the URL
URL url=new url ("http://www.baidu.com");
System.out.println (Url.getprotocol ());
System.out.println (Url.gethost ());
returns 1 if no port specified in//url
System.out.println (Url.getport ());
The default port for the protocol is not specified in//url, and no default port for that protocol is returned-1
System.out.println (Url.getdefaultport ());
//Difference
Url.getfile (); //Return path information with query parameters
Url.getpath (); //return path only
//Gets the contents of the URL (the HTTP protocol does not contain header content or other protocol content)
InputStream input= Url.openstream ();
BufferedReader reader=new BufferedReader (new InputStreamReader (Input, "utf-8")); //Different URLs may not encode the same
String Tmp=null;
while ((Tmp=reader.readline ())!=null) {
SYSTEM.OUT.PRINTLN (TMP);
}
Gets the underlying connection object in the URL, which can be used to get more information and operations
//For example, the direct URL gets the content default does not time out, this may cause the program blocking, through the connection object can set the timeout time
URLConnection connection= url.openconnection ();
//Encode a special string in a URL
Urldecoder.decode ("xxxx", "utf-8");
Urlencoder.encode ("xxxx", "utf-8");
URI does not have network get function
Uri uri=new uri ("http://www.baidu.com");
System.out.println (Uri.gethost ());
System.out.println (Uri.getport ());
Java Network programming 2-url and URIs