Java Advanced Programming URL handling

Source: Internet
Author: User

Java URL Processing

URL (Uniform Resource Locator) Chinese name is Uniform Resource Locator, sometimes also known as the Web address. Represents a resource on the Internet, such as a Web page or an FTP address.

In this section we will describe Java as handling URLs. The URL can be divided into the following sections.

protocol://host:port/path?query#ref

The Protocols (protocol) can be HTTP, HTTPS, FTP, and file. Port is the port number. Path is the file path and filename.

The URL instance of the HTTP protocol is as follows:

http://www.cnblogs.com/toutou/

The above URL instance does not specify a port because the HTTP protocol has a default port number of 80.

URL class method

A URL class is defined in the java.net package that is used to process the contents of the URL. For the creation and use of URL classes, the following are described separately.

Java.net.URL provides a rich way to build URLs and can get resources through Java.net.URL.

Serial Number Method Description
1 The public URL (string protocol, string host, int port, string file) throws Malformedurlexception.
Creates a URL from a given parameter (protocol, hostname, port number, file name).
2 Public URL (String protocol, string host, String file) throws Malformedurlexception
Creates a URL using the specified protocol, host name, file name, and port using the default port for the protocol.
3 Public url (String URL) throws Malformedurlexception
Creates a URL from a given URL string
4 Public URL (url context, String URL) throws Malformedurlexception
Create using base address and relative URL

The URL class contains a number of methods for accessing the various parts of the URL, in the following ways and descriptions:

Serial Number Method Description
1 Public String GetPath ()
Returns the URL path section.
2 Public String Getquery ()
Returns the URL query section.
3 Public String getauthority ()
Gets the authorization portion of this URL.
4 public int Getport ()
Return to the URL port section
5 public int Getdefaultport ()
Returns the default port number for the protocol.
6 Public String Getprotocol ()
The protocol that returns the URL
7 Public String GetHost ()
Returns the host of the URL
8 Public String GetFile ()
Returns the URL file name section
9 Public String GetRef ()
Gets the anchor point for this URL (also known as a "reference").
10 Public URLConnection OpenConnection () throws IOException
Open a URL connection and run the Client access resource.
Instance

The above example shows the use of the Java.net URL class to get the various parts of the URL parameters:

//file name: Urldemo.javaImportjava.net.*;ImportJava.io.*; Public classurldemo{ Public Static voidmain (String [] args) {Try{URL URL=NewURL ("Http://www.cnblogs.com/index.html?language=cn#j2se"); System.out.println ("URL is" +url.tostring ()); System.out.println ("Protocol is" +Url.getprotocol ()); System.out.println ("Authority is" +url.getauthority ()); System.out.println ("File name is" +url.getfile ()); System.out.println ("Host is" +url.gethost ()); System.out.println ("Path is" +Url.getpath ()); System.out.println ("Port is" +Url.getport ()); System.out.println ("Default port is" +Url.getdefaultport ()); System.out.println ("Query is" +url.getquery ()); System.out.println ("Ref is" +url.getref ()); }Catch(IOException e) {e.printstacktrace (); }   }}

The results of the above example compilation run as follows:

URL is Http://www.cnblogs.com/index.html?language=cn#j2seprotocol are httpauthority is www.cnblogs.comfile name is/ Index.htm?language=cnhost is Www.amrood.compath is/index.htmlport is-1default port is 80query are Language=cnref is J2SE
Urlconnections class method

OpenConnection () returns a java.net.URLConnection.

For example:

    • If you connect the URL of the HTTP protocol, the OpenConnection () method returns the HttpURLConnection object.

    • If the URL you are connecting to is a JAR file, the OpenConnection () method returns the Jarurlconnection object.

    • Wait a minute...

The list of URLConnection methods is as follows:

Serial Number Method Description
1 Object getcontent ()
Retrieving URL link content
2 Object getcontent (class[] classes)
Retrieving URL link content
3 String getcontentencoding ()
Returns the header content-encoding field value.
4 int Getcontentlength ()
Return header content-length field value
5 String getContentType ()
Return header Content-type field value
6 int getlastmodified ()
Returns the header last-modified field value.
7 Long Getexpiration ()
Returns the header Expires field value.
8 Long Getifmodifiedsince ()
Returns the Ifmodifiedsince field value of an object.
9 public void Setdoinput (Boolean input)
URL connections can be used for input and/or output. If you intend to use a URL connection for input, set the DOINPUT flag to true, or set to False if you do not intend to use it. The default value is true.
10 public void Setdooutput (Boolean output)
URL connections can be used for input and/or output. If you intend to use a URL connection for output, set the DOOUTPUT flag to true, or set to False if you do not intend to use it. The default value is False.
11 Public InputStream getInputStream () throws IOException
Returns the input stream of the URL used to read the resource
12 Public OutputStream Getoutputstream () throws IOException
Returns the output stream of the URL for writing to the resource.
13 Public URL GetURL ()
Returns the URL of the URLConnection object connection
Instance

The URL in the following instance uses the HTTP protocol. OpenConnection returns the HttpURLConnection object.

//file name: Urlconndemo.javaImportjava.net.*;ImportJava.io.*; Public classurlconndemo{ Public Static voidmain (String [] args) {Try{URL URL=NewURL ("http://www.cnblogs.com"); URLConnection URLConnection=url.openconnection (); HttpURLConnection Connection=NULL; if(URLConnectioninstanceofhttpurlconnection) {Connection=(httpurlconnection) URLConnection; }         Else{System.out.println ("Please enter an HTTP URL."); return; } BufferedReader in=NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); String urlstring= "";         String current;  while(current = In.readline ())! =NULL) {urlstring+=Current ;      } System.out.println (urlstring); }Catch(IOException e) {e.printstacktrace (); }   }}

The results of the above example compilation run as follows:

$ java urlconndemo.....a complete HTML content of home page of amrood.com .....

Java Advanced Programming URL handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.