Jakarta Commons: Using class and the assembly (3) OF: Vikram Goyal; cactus studio Translation issued Time: 2003.08.07 15:33:09 3.2 HttpClient
■ Overview: This API extends the Java.net package, providing the function of analog browser.
■ Official resources: home page, binary, source code.
■ When applies: When you want to construct a web browser; when your application needs an efficient way to communicate with HTTP / HTTPS communication.
■ Example Application: httpclientDemo.java. It is required to have commons-httpclient.jar in ClassPath, Common-logging.jar. Requirements to use JDK 1.4 or higher.
■ Description:
HTTPClient extension and enhances standard Java.net packages, is a wide range of content, which is extremely rich, and can construct various distributed applications that use HTTP protocols, or can also be embedded to existing applications, add access to the application. HTTP The ability of the agreement. In the Commons Stabilization, the HTTPClient documentation seems to be more perfect than other packages, and there are several examples. Below we understand how to extract a web page through a simple example, there is also a similar example in the HTTPClient document, and we will expand that example to support SSL. Note This example requires JDK 1.4 support because it uses the Java Secure Socket Connection library, and this library is only available in JDK 1.4 and higher.
1 First determine a page that can be downloaded by HTTPS, this example is https://www.paypal.com/. Make sure that the% java_home% / jre / lib / security / java.security file contains the following line code: security.provider.2 = com.sun.net.ssl.internal.ssl.Provider.
In addition to these settings, the HTTPS connection has no other particular place - at least for this example. However, if the root certificate used by the remote website is not recognized by the Java you use, you must first import its certificate.
2 Create an instance of HTTPClient. The HTTPCLIENT class can be seen as the main drive of the application, and all the functions of the network relies on it. The HttpClient class requires a Connection Manager to manage connections. HTTPConnectionManager allows us to create its own connection manager, or we can also use built-in SimpleHttpConnectionManager or MultithreadedhttpConnectionManager class. If you do not specify a connection manager when you create HTTPCLIENT, HttpClient uses SimplehttpConnectionManager by default.
// Create an instance of httpclient httpclient client = new httpclient ();
3 Create an instance of httpMethod, which is determined which transmission method for communication with the remote server, HTTP allows the transmission methods including: GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE. These transmission methods are implemented as an independent class, but all of these classes implements HTTPMETHOD interfaces. In this example, we use getMethod to specify the URL we want GET in the parameter when you create a getMethod instance.
// Create an instance of httpMethod httpmethod method = new getMethod (URL); 4 Execute the extraction operation defined by the HTTPMETHOD. After the execution is completed, the ExecuteMethod method will return the status code of the remote server report. Note that ExecuteMethod is httpclient instead of httpmethod.
// Execute the extraction of HTTPMethod definition statuscode = client.executeMethod (METHOD);
5 Read the response returned by the server. If the previous connection operation fails, the program will encounter HTTPEXCEPTION or IOException, where IOException generally means that the network is wrong, and continues to try is not likely to succeed. The response returned by the server can be read according to a variety of ways, such as an array, as an input stream, or as a string. After getting the response returned by the server, we can dispose of it according to your needs.
Byte [] responsebody = method.getResponsebody ();
6 The last thing to do is to releasing the connection.
Method.releaseConnection ();
The above is only very simple to introduce the HTTPClient library, and the actual functionality of httpclient is much more rich than this article, not only robust and efficient, please refer to the API documentation.
3.3 Net
■ Overview: A underlying API for operating the Internet underlying protocol.
■ Official resources: home page, binary, source code.
■ When you apply: When you want to access a variety of Internet underlying protocols (Finger, WHOIS, TFTP, Telnet, POP3, FTP, NNTP, and SMTP).
■ Sample application: NetDemo.java. The ClassPath is required to include Commons-Net-1.0.0.jar.
■ Description:
The NET package is a powerful, professional class library, and the class in the class library is originally a commercial product called NetComponents.
The NET package not only supports access to a variety of low-level protocols, but also provides a high-level abstraction. In most cases, the abstraction provided by the NET package can meet the general needs, which makes developers no longer need to directly face the low-level commands of the Socket level of the various protocols. Using high-level abstraction does not reduce any feature, NET API is well done in this regard, providing adequate features, not too much compromise in feature.
SocketClient is a fundamental class that supports all protocols, which is an abstract class that aggregates public functions required by various protocols. The use of various agreements is actually very similar. First, use the Connect method to establish a connection to the remote server, perform the necessary operation, and finally terminate the connection with the server. The specific use steps are described below by example.
// ... // 1 Create a client. We will use nntpclient // to download newsgroups from the news server. Client = new nntpclient (); // ... // 2 Connect to the news server with the client created. // This is a small server that is a small newsgroup. Client.connect ("Aurelia.deine.Net"); // ... // 3 Extract the list of newsgroups. The following command will return an array of // newsGroupInfo objects. If you do not include a newsgroup on the specified suit // manager, the returned array will be empty, // If an error is encountered, the return value is NULL. List = client.listnewsgroups (); //...// 4 Finally terminate the connection to the server. if (client.isconnected ()) client.disconnect (); must be explained, the listnewsgroups command may take a long time to return, on the one hand because the network speed is affected, it may be due to the list of newsgroups huge. The newsgroupinfo object contains detailed information about the newsgroup and provides a command of the operation newsgroup, such as extracting the total number of articles, the last published article, the permission to publish the article.
Other clients, such as fingerclient, pop3client, telnetClient, etc., usage is similar.
Conclusion: The introduction to the web-related class and other classes is over. In the next article, we will explore the XML classes and packaging classes, and the last article describes the tool class.
I hope the reader is interested in trying the program example provided in this paper. Many times Jakarta Commons give people a feeling of confusion, I hope this article has deepened you to learn from Jakarta Commons, or at least in your interest in the Commons sub-project and it provides the various practical APIs and libraries.
Please download this article from this article: jakartacommons1_code.zip
(Editor: Zhang Mingyan)