Next, we'll take a look at the second topic of Web Programming on Java: URL processing. The URL (uniform Resource Locator) Chinese name is a 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. This time we'll see that Java is like processing URLs. URLs can be divided into the following sections:
Protocol://host:port/path?query#fragment
Protocol (Protocol) can be HTTP, HTTPS, FTP, and File,port as the port number, path is the file path and file name. The URL instance of the HTTP protocol is as follows:
Http://www.luyaran.com/index.html?language=cn#j2se
Take a look at the parsing of URLs:
protocol for (Protocol):http host (host:port): www.luyaran.com Port : 80, the above URL instance does not specify a port because HTTP The default port number for the protocol is 80. file path is (path):/index.html Request parameters (query): LANGUAGE=CN location (fragment):j2se, navigate to the page with the id attribute to J2SE The position of the HTML element.
then look at the URL class method. The URL class is defined in the java.net package, which is used to handle the content 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 to 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, host name, port number, filename). |
2 |
Public URL (String protocol, string host, String file) throws Malformedurlexception Creates a URL using the specified protocol, host name, file name, and the port uses the default port for the protocol. |
3 |
Public URL (String URL) throws Malformedurlexception Creates a URL from a given URL string |
4 |
Public URLs (URL context, String URL) throws Malformedurlexception Create using base address and relative URL |
The URL class contains a number of methods used to access the various parts of the URL, and the method and description are as follows:
Serial Number |
Method Description |
1 |
Public String GetPath () Returns the URL path part. |
2 |
Public String Getquery () Returns the URL query section. |
3 |
Public String getauthority () Gets the authorization portion of this URL. |
4 |
public int Getport () Returns the URL port part |
5 |
public int Getdefaultport () Returns the default port number for the protocol. |
6 |
Public String Getprotocol () Return protocol for URL |
7 |
Public String GetHost () Host that returns the URL |
8 |
Public String GetFile () Return URL file name part |
9 |
Public String GetRef () Gets the anchor point (also known as "Reference") for this URL. |
10 |
Public URLConnection OpenConnection () throws IOException Open a URL connection and run the Client access resource. |
The following example shows the URL class using java.net to get the various partial parameters of the URL:
Import java.net.*;
Import java.io.*;
public class Urldemo
{
public static void Main (String [] args)
{
Try
{
URL url = new URL ("Http://www.luyaran.com/index.html?language=cn#j2se");
System.out.println ("URL is:" + url.tostring ());
SYSTEM.OUT.PRINTLN ("agreement is:" + url.getprotocol ());
SYSTEM.OUT.PRINTLN ("Authentication information:" + url.getauthority ());
SYSTEM.OUT.PRINTLN ("FileName and request parameters:" + url.getfile ());
SYSTEM.OUT.PRINTLN ("Host name:" + url.gethost ());
System.out.println ("Path:" + Url.getpath ());
System.out.println ("Port:" + url.getport ());
SYSTEM.OUT.PRINTLN ("Default port:" + url.getdefaultport ());
SYSTEM.OUT.PRINTLN ("Request parameter:" + url.getquery ());
System.out.println ("Location:" + url.getref ());
}catch (IOException e)
{
E.printstacktrace ();
}
}
}
the results of the operation are:
URL is: http://www.luyaran.com/index.html?language=cn#j2se
protocol is: HTTP
authentication information: www.runoob.com
filename and request parameter:/ INDEX.HTML?LANGUAGE=CN
host Name: www.runoob.com
Path:/index.html
port: -1
Default port:
Request Parameter: LANGUAGE=CN Location
: J2SE
then look at the Urlconnections class method. OpenConnection () returns a java.net.URLConnection. are as follows:
If you connect to the HTTP protocol URL, 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 URLConnection method list 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. Set the DOINPUT flag to true if you intend to enter using a URL connection, or 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. Set the DOOUTPUT flag to true if you intend to use a URL connection for output, or False if you do not intend to use it. The default value is False. |
11 |
Public InputStream getInputStream () throws IOException Returns the input stream for a URL that is used to read resources |
12 |
Public OutputStream Getoutputstream () throws IOException Returns the output stream for the URL to write to the resource. |
13 |
Public URL GetURL () Returns the URL of a URLConnection object connection |
The URL in the following instance uses the HTTP protocol. OpenConnection returns the HttpURLConnection object:
Import java.net.*;
Import java.io.*;
public class Urlconndemo
{
public static void Main (String [] args)
{
Try
{
URL url = new URL ("http://www.luyaran.com");
URLConnection urlconnection = Url.openconnection ();
HttpURLConnection connection = null;
if (URLConnection instanceof httpurlconnection)
{
Connection = (httpurlconnection) urlconnection;
}
Else
{
System.out.println ("Please enter URL address");
Return
}
BufferedReader in = new BufferedReader (
New InputStreamReader (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 operation are:
$ javac Urlconndemo.java
$ java urlconndemo .....
This will output the HTML content of the page (http://www.luyaran.com) ...
Well, that's it for the time being. If the feeling is good, please give a lot of praise support oh ...