java Net---------------------------URL classes
package java_net;import java.net.malformedurlexception;import java.net.url;/* * Test URL class */public class url_test { public static void main (String[] args) {try {//We need to construct a URL object that has many kinds of/* * 1, constructs a URL object from a string * public URL (String spec) throws malformedurlexception * creates a url object from the string representation. * this constructor is equivalent to a call to the two-argument constructor with a null First argument. * parameters: * spec - the string to parse as a url. (parameter spec is a string that can represent a network address) */url url1 = new url ("http://www.baidu.com"); System.out.println (Url1.getauthority ());/* * gets the protocol name of this url * Returns the name of the Protocol */system.out.println ("protocol is:" +url1.getprotocol ());//Output HTTP protocol/* * gets the host name of this url * returns the name of the URL host */system.out.println ("host is:" +url1.getHost ());/* * gets the path part of this url. * gets a partial path to the URL address, Null */system.out.println (Url1.getpath ()) if not present;/* * gets the file name of this URL. * gets the file name of the address, output null */system.out.println (Url1.getfile ()) when no file is specified;/* * Gets the anchor (also known as the "reference") of this URL. * Returns:the anchor (also known as the "reference") of this url, or null if one does not exist * returns the value of the anchor point, Non-existent then empty */system.out.println (Url1.getref ());/* * gets the port number of this URL. * Gets the port number, does not set the output -1 */system.out.println ("Port number is:" +url1.getport ());/* * gets the Default port number of the protocol associated with this url. * if the url scheme or the urlstreamhandler for the url do not define a default port number, * then -1 is returned. * returns the default port number of the Protocol, if the Protocol does not define a port number, returns -1 */SYSTEM.OUT.PRINTLN (Url1.getdefaultport () );} catch (malformedurlexception e) {e.printstacktrace ();}}
There are other ways to see the URL, when you can check the API!
Instance:
package java_net;import java.io.bufferedreader;import java.io.ioexception;import Java.io.inputstream;import java.io.inputstreamreader;import java.net.malformedurlexception;import java.net.URL;/* * read data in the network via a URL * read www.baidu.com home */public class Url_test02 {public static void main (String[] args) {try {//Create a URL object url Url = new url ("https://www.baidu.com");//Gets the byte input stream of the URL object inputstream is = Url.openstream ();//wraps the byte input stream into a character input stream, noting that you want to specify a character set Inputstreamreader isr = new inputstreamreader (is , "Utf-8");//wraps the character input stream into a buffered character input stream Bufferedreader br = new bufferedreader (ISR);//reads the data in string line = br.readline ();//loop read data while (Line!=null) {//print read Data System.out.println (line);//Read Line again = br.readline ();} Close input stream br.close (); Isr.close (); Is.close ();} catch (malformedurlexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}}
This article is from the "@coder" blog, be sure to keep this source http://smallcoder.blog.51cto.com/11941149/1859827
java Net--------------------------------URL classes