Research on Uri and URL conversion to Java Network Programming (II)

Source: Internet
Author: User
In the previous topic Uri and URL of Java Network Programming (I), we introduced the concept and architecture of Uri and URL, in this article, I will continue to introduce how to use URL and mime (multi-purpose Internet Mail Extension Protocol) and how it relates to a URL.

  Use URL

By providing URL classes, network APIs allow us to use URLs at the source code layer. Each URL object encapsulates the resource identifier and protocol handler. The previous technique shows that one of the ways to obtain a URL object is to call the tourl () method of the URI object. However, this option is not convenient (why do you need to create a URI object when you need a URL object ?). Instead, you can call the URL constructor to create a URL object. You can also call the URL method to extract the URL component and open an input stream to read information from the resource to obtain reference of an object that can easily retrieve resource data, compare the URLs of two URL objects to obtain the connection object of the resource. This connection object allows the code to learn (and write) more resource information.

The URL class has six constructors. The simplest one is URL (string URL), which has a string type parameter. It splits the URL into its own component and stores these components in a new URL object. If a URL does not contain a protocol handler or the URL protocol is unknown, the other five constructors will generate a java.net. malformedurlexception object.
The following code snippet demonstrates how to use a URL (string URL) to create a URL object. This object encapsulates a simple URL component and an HTTP protocol processing program.

URL url = new URL ("http://www.informit.com ");

Once you have a URL object, you can use getauthority (), getdefaultport (), GetFile (), gethost (), getpath (), getport (), getprotocol (), getquery (), getref (), and getuserinfo (). the getdefaultport () and other methods to extract various components. If no port is specified in the URL, the getdefaultport () method returns the default port used by the protocol handler (resource location) of the URL object. The combination of the returned path of the GetFile () method and the query component. The getprotocol () method returns the name of the Protocol that determines the connection type of the resource (such as HTTP, mailto, and FTP. The getref () method returns part of the URL (the reference we know ). Finally, the getuserinfo () method returns the user information section of the authorization organization component. In the URL component extraction method, if some components do not exist (if the protocol handler of the URL object does not specify the default port, it also returns-1 ), these methods return null or-1.

As a supplement to the extraction methods of these components, you can also call the openstream () method to retrieve java. Io. inputstream references. With this reference, you can read resources in byte-oriented mode.

List 4 is the source code of urldemo1. This program creates a URL object from the command line parameters, calls the URL Component Extraction Method to retrieve the URL component, and calls the URL's openstream () method to open the connection to the resource and return an inputstream reference for reading byte data from the resource, read/print these bytes, and close the input stream.

List 4: urldemo1.java

// Urldemo1.java
Import java. Io .*;
Import java.net .*;

Class urldemo1
{
Public static void main (string [] ARGs) throws ioexception
{
If (ARGs. length! = 1)
{
System. Err. println ("Usage: Java urldemo1 URL ");
Return;
}

URL url = new URL (ARGs [0]);

System. Out. println ("authority =" + URL. getauthority ());
System. Out. println ("Default port =" + URL. getdefaport port ());
System. Out. println ("file =" + URL. GetFile ());
System. Out. println ("host =" + URL. gethost ());
System. Out. println ("Path =" + URL. getpath ());
System. Out. println ("Port =" + URL. getport ());
System. Out. println ("protocol =" + URL. getprotocol ());
System. Out. println ("query =" + URL. getquery ());
System. Out. println ("ref =" + URL. getref ());
System. Out. println ("User Info =" + URL. getuserinfo ());

System. Out. Print ('/N ');

Inputstream is = URL. openstream ();

Int ch;
While (CH = is. Read ())! =-1)
System. Out. Print (char) CH );

Is. Close ();
}
}

Enter Java urldemo1 http://www.javajeff.com/articles/articles/htmlin the command line. The output of the Code is as follows:

Authority = http://www.javajeff.com
Default port = 80
File =/articles/articles.html
Host = http://www.javajeff.com
Path =/articles/articles.html
Port =-1
Protocol = http
Query = NULL
Ref = NULL
User Info = NULL

<HTML>
<Head>
<Title>
Java Jeff-Articles
</Title>

<Meta http-equiv = Content-Type content = "text/html;
Charset = ISO-8859-1 ">
<Meta name = author content = "Jeff Friesen">
<Meta name = keywords content = "Java, Virtual Machine">

<Script language = JavaScript>
If (navigator. appname = "Netscape ")
Document. Write ("<br> ");
</SCRIPT>
</Head>

<Body bgcolor = #000000>
<Center>
<Table border = 1 cellpadding = 5 cellspacing = 0>
<Tr>
<TD>
<Table cellpadding = 0 cellspacing = 0>
<Tr>
<TD>
<A href = informit/informit.html>
</a>
</TD>
</Tr>
</Table>
</TD>

<TD align = middle>
<br>

<A href = ../welcome/welcome.html>

</A> <br>

<br>

<A href = ../ADS/ads.html>
Src?jupiter.jpg>
</TD>

<TD>
<Table cellpadding = 0 cellspacing = 0>
<Tr>
<TD>
<A href = javaworld/javaworld.html>
</a>
</TD>
</Tr>
</Table>
</TD>
</Tr>
</Table>
</Center>

<Br>
<Font color = # ffffff>
<Center>
Best viewed at a resolution of 1024x768 or higher. <br>

<br>

<I>
Copyright & copy; 2001-2002, Jeff Friesen. All rights
Reserved.
</I>

<P>
<A href = ../index.html>
</a>
</Center>
</Font>
</Body>
</Html>

In the above information, the output identifier 80 is the default port, and HTTP is the protocol. The above shows the source code of the output HTML page.
The openstream () method of the URL usually returns the reference of an object created by a specific subclass of the abstract inputstream class. This means that you must read resource data in byte order. This is appropriate because you do not know the type of data to be read. If you know in advance that the data to be read is text and each row ends with a line break (/N), you can read data by row instead of byte.

The following code snippet demonstrates packaging an inputstream object into Java. io. the inputstreamreader object transitions from 8-bit to 16-bit, and wraps the result object into Java. io. the bufferedreader object accesses the Readline () method of bufferedreader and calls the Readline () method to read all rows of text from the resource.

Inputstream is = URL. openstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (is ));
String line;
While (line = Br. Readline ())! = NULL)
System. Out. println (line );
Is. Close ();

Sometimes it is not convenient to read data in byte order. For example, if the resource is a jpeg file, it is better to obtain an image processing process and register a user to use the data. It is not difficult to display the image immediately after it is downloaded. If this happens, you need to use the getcontent () method.

When the getcontent () method is called, it will return the object reference of an object, and you can call the method of this object (after conversion to an appropriate type ), use a more convenient way to retrieve data. However, before calling this method, you must use instanceof to verify the object type to prevent class exceptions.

For JPEG resources, getcontent () returns an object. The class of this object implements the java. AWT. image. imageproducer interface. The following code snippet demonstrates how to use instanceof to verify that the object is imageproducer and convert it. Next, you can call the imageproducer method to register a user and initialize the image usage process.

URL url = new URL (ARGs [0]);
Object o = URL. getcontent ();
If (O instanceof imageproducer)
{
Imageproducer IP = (imageproducer) O;
//...
}

  Tips

Call the equals (Object O) and samefile (Object O) Methods of the URL to determine whether the two URLs are the same. The first method contains a comparison segment, but the second method does not. You can refer to the SDK documentation for more information.

Check the source code of the getcontent () method. You will find openconnection (). getcontent (). In addition, check the source code of the openstream () method and you will find openconnection (). getinputstream (). Each method first calls the openconnection () method of the URL. This method returns the reference of an object created by a subclass of the abstract java.net. urlconnection class (describing the connection to certain resources. The urlconnection method reflects the details of resources and connections, so that we can write code to write information to resources.

The urldemo2 source code in List 5 demonstrates openconnection () and calls some urlconnection methods.

List 5: urldemo2.java

// Urldemo2.java

Import java. Io .*;
Import java.net .*;
Import java. util .*;

Class urldemo2
{
Public static void main (string [] ARGs) throws ioexception
{
If (ARGs. length! = 1)
{
System. Err. println ("Usage: Java urldemo2 URL ");
Return;
}

URL url = new URL (ARGs [0]);

// Return the reference of the new protocol object representing the connection of a resource

Urlconnection UC = URL. openconnection ();

// Connect

UC. Connect ();

// Print the content of multiple header fields

Map M = UC. getheaderfields ();
Iterator I = M. entryset (). iterator ();

While (I. hasnext ())
System. Out. println (I. Next ());

// Find the resource if the resource allows input and output operations

System. Out. println ("input allowed =" + UC. getdoinput ());

System. Out. println ("output allowed =" + UC. getdooutput ());
}
}

After an openconnection () call is returned, the connect () method is called to establish a connection to a certain resource. (Although the openconnection () method returns a reference to a connection object, openconnection () does not connect to resources ). The getheaderfields () method of urlconnection returns an application of an object. The class of this object implements the java. util. map interface. This chart (MAP) contains a set of header names and values. What is a header )? The header is a text-based name/value pair that identifies the type and length of resource data.

After urldemo2 is compiled, enter the Java urldemo2 http://www.javajeff.com in the command line and the output is as follows:

Date = [Sun, 17 Feb 2002 17:49:32 GMT]
Connection = [keep-alive]
Content-Type = [text/html; charset = iso-8859-1]
Accept-ranges = [bytes]
Content-Length = [7214]
Null = [http/1.1 200 OK]
Etag = ["4470e-1c2e-3bf29d5a"]
Keep-alive = [timeout = 15, max = 100]
Server = [Apache/1.3.19 (UNIX) Debian/GNU]
Last-modified = [wed, 14 Nov 2001 16:35:38 GMT]
Input allowed = true
Output allowed = false

The above output recognizes many headers (including date, null, Content-Length, server, last-modified, and so on) and their values. The output also shows that only data can be read from the resource.

Are you surprised how a Program identifies resource data? Take a closer look at the previous output and you will see something called Content-Type. Content-Type is a header that identifies the type of resource data (content) as text/html. Text is what we know, and HTML is what we know. (If the content is plain text, the value of Content-Type may be text/plain. The above type indicates that the content is text, but it is not in no format ). The Content-Type header is part of the multi-purpose Internet Mail Extension (MIME) We know.

Mime is an extension of the traditional 7-bit ASCII standard for message transmission. By introducing multiple headers, mime combines texts of videos, sounds, images, and different character sets with 7-bit ASCII. With Content-Type, mime can recognize Content-Length and other standard headers. When you use the urlconnection class, you will encounter getcontenttype () and getcontentlength (). These methods return Content-Type and Content-Length headers.

You may have heard of HTML forms (<form>, </form>) and other HTML tags. Form allows us to get (get) data from a resource and send (post) The field data of the HTML form to a certain resource according to subsequent processing. You can use the urlconnection class and mime simulation to obtain and send the HTML form of data. The following describes how to complete such a transaction.

Suppose you want to send form data (post) to a server program. The operation to send the form data. First, the data in the form must be organized into name/value pairs (name/Value Pair), and then each pair must be specified in name = value format. If multiple name/value pairs are sent again, each pair must be separated using the & Symbol. The final name content and value content must be encoded using the application/X-WWW-form-urlencoded MIME type. For example, x = Y & A = B represents two name/value pairs -- x/y and A/B.
To assist coding, Java provides the java.net. urlencoder class, which declares a pair of static encode () methods. Each method has a string parameter and returns a reference to a string object containing the encoded parameter content. For example, if encode () finds that there is a space in the parameter, it uses the plus sign in the result to replace the space.

The following code snippet demonstrates how to call urlencoder's encode (string s) method to encode a Space B string. Result A + B is stored in a New String object, and result references it.

String result = urlencoder. encode ("a B ");

As a supplement to the form data, you must tell the urlconnection object that the data has been sent, because the default operation of urlconnection is to obtain data. To complete this transaction, You can first convert the return value of openconnection () to the httpurlconnection type (after ensuring that the returned value is of the correct type ). Call the setrequestmethod (string method) method of the result object and use post as the value of the object referenced by the method parameter.

Another required transaction is to call the setdooutput (Boolean dooutput) method of urlconnection. Its parameter value must be true. This transaction is necessary because the urlconnection object does not support output by default. (Then, the program can finally call the getoutputstream () method of urlconnection to return a reference to the resource output stream for the sent form data ).

LIST 6 is the source code of urldemo3. It demonstrates sending form data to a resource of the "Understanding" application/X-WWW-form-urlencoded content type. It implements various transactions mentioned above.

LIST 6: urldemo3.java

// Urldemo3.java

Import java. Io .*;
Import java.net .*;

Class urldemo3
{
Public static void main (string [] ARGs) throws ioexception
{
// Check the number of the last two parameters and the number of parameters

If (ARGs. Length <2 | args. Length % 2! = 0)
{
System. Err. println ("Usage: Java urldemo3 name value" +
"[NAME value...]");
Return;
}

// Create a program to connect to the server program resource URL object, which returns a form name/value pair

URL;
Url = new URL
("Http://banshee.cs.uow.edu.au: 2000 /~ Nabg/echo. cgi ");

// Return an HTTP resource connection reference to a specific protocol object

Urlconnection UC = URL. openconnection ();

// Verify the connection type, which must be httpurlconnection

If (! (UC instanceof httpurlconnection ))
{
System. Err. println ("wrong connection type ");
Return;
}

// Indicates that the program must output the name/value pair to the server program resource.

UC. setdooutput (true );

// Indicates that only useful information can be returned.

UC. setusecaches (false );

// Set the Content-Type header to indicate the MIME type of the specified URL encoded data form

UC. setrequestproperty ("Content-Type ",
"Application/X-WWW-form-urlencoded ");

// Create a name/value to send the value to the server

String content = buildcontent (ARGs );

// Set the Content-Type header to indicate the MIME type of the specified URL encoded data form

UC. setrequestproperty ("Content-Length ",
"" + Content. Length ());

// Extract the appropriate connection type

Httpurlconnection Hc = (httpurlconnection) UC;

// Set the HTTP Request Method to post (the default is get)

HC. setrequestmethod ("Post ");

// Output content

Outputstream OS = UC. getoutputstream ();
Dataoutputstream dos = new dataoutputstream (OS );
Dos. writebytes (content );
Dos. Flush ();
Dos. Close ();

// Input and display content from the server program resource

Inputstream is = UC. getinputstream ();

Int ch;
While (CH = is. Read ())! =-1)
System. Out. Print (char) CH );

Is. Close ();
}

Static string buildcontent (string [] ARGs)
{
Stringbuffer sb = new stringbuffer ();

For (INT I = 0; I <args. length; I ++)
{
// Encode the parameters for correct transmission

String encodeditem = urlencoder. encode (ARGs [I]);

SB. append (encodeditem );

If (I % 2 = 0)
SB. append ("="); // separation name and Value
Else
SB. append ("&"); // separation name/value pair
}

// Delete the last & delimiter

SB. setlength (sb. Length ()-1 );

Return sb. tostring ();
}
}

You may wonder why urldemo3 did not call the connect () method of urlconnection. This method is not explicitly called, because if the connection to the resource is not established, other urlconnection methods (such as getcontentlength () will explicitly call the connect () method. However, once the connection is established, calling these methods (such as setdooutput (Boolean dooutput) violates the rules. After connect () is called (explicitly or implicitly), these methods generate an illegalstateexception object.
After urldemo3 is compiled, enter Java urldemo3 name1 value1 name2 value2 name3 value3 in the command line. You can see the following output:

<HTML> <Title> echoing your name value pairs </title>
</Head>
<Body>
<Ol>
<Li> name1: value1
<Li> name2: value2
<Li> name3: value3
</OL>
<HR>
Mon Feb 18 08:58:45 2002
</Body>
</Html>

The output of the server program resources is composed of HTML, which responds to name1, value1, name2, value2, name3, and value3.

  Tips

If you need the string representation of the URL object URL, call toexternalform () or tostring (). The functions of the two methods are the same.

  Summary

This article studies Java Network APIs, focusing on Uri, URL, and urn. You have learned these concepts and how to use the URI and URL (URL-related) classes. At the same time, you have learned about mime and Its Relationship with URL. Now you should write some code to familiarize yourself with what you have learned.

Related Article

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.