Tag:java url Learning summary
string host = "www.java2s.com"; string file = "/index.html"; string[] schemes = {"http", "https", "ftp", "mailto", "telnet", "file" , "LDAP", "gopher", "jdbc", "rmi", "Jndi", "Jar", "Doc", "NetDoc", "NFS", "verbatim", "finger", "daytime", "Systemresource"};for (int i = 0; i < schemes.length; i++) { try { url u = new url (Schemes[i], host, file); system.out.println (schemes[i] + " is supported/r/n"); } catch (Exception ex) { System.out.println (schemes[i] + " is not supported/r/n "); }}
Result is
HTTP is Supported/r/nhttps are Supported/r/nftp is Supported/r/nmailto are supported/r/ntelnet is isn't supported/r/nfile is s Upported/r/nldap is not supported/r/ngopher are not supported/r/njdbc are not supported/r/nrmi are not Supported/r/njndi are N OT Supported/r/njar is Supported/r/ndoc are not Supported/r/nnetdoc are Supported/r/nnfs are not supported/r/nverbatim are not Supported/r/nfinger are not supported/r/ndaytime are not supported/r/nsystemresource are not supported/r/n
Url.getcontent () Returns an object that can be used with. GetClass (). GetName () to get the actual name of this object, as in the following code: (here, when creating a URL object, the value passed to the constructor method must be This protocol begins with an exception to be thrown: Java.net.MalformedURLException:no protocol:www.baidu.com)
URL url = new URL ("http://www.baidu.com"); Object content = Url.getcontent (); System.out.println (Content.getclass (). GetName ());
The above code prints:
Sun.net.www.protocol.http.httpurlconnection$httpinputstream
You can use the OpenConnection method of the URL to return a URLConnection object that can be used to get the input stream.
URL url = new URL ("http://www.baidu.com"); URLConnection urlconnection = Url.openconnection (); InputStream InputStream = Urlconnection.getinputstream (); int C; while ((c = Inputstream.read ())! =-1) {System.out.println ((char) c);} Inputstream.close ();
Or get the input stream using the OpenStream method of the URL itself.
URL url = new URL ("http://www.baidu.com");//Opens a connection to this URL and returns a inputstream that is used to read from the connection. InputStream in = Url.openstream (); int C;while ((c = In.read ())! =-1) System.out.print (c); In.close ();
Java.net.URL Study Summary