Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the following three classes in the Java.net package

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use the following three classes in the Java.net package, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the details of this article.

Introduction

The Java platform implements Java Socket in the java.net package. We will use the following three classes in the Java.net package to work:

URLConnection

Socket

ServerSocket

There are more classes in the java.net package, but these are the ones you encounter most often, so let's start with URLConnection, which provides a way to use Socket in your java code without understanding the underlying mechanisms of Socket.

You can use sockets without even trying.

Create a URLConnection

Configure it with different setter methods

Connect to URLConnection

Interact with different getter methods

Next, let's use some examples to demonstrate how to use URLConnection to request a document from a server.

URLClient class

We'll start with the structure of the URLClient class.

Import java.io.*;import java.net.*;public class URLClient {protected URLConnection connection; public static void main (String [] args) {} public String getDocumentAt (String urlString) {}}

Note: java.net and java.io packages must be imported first.

We give our class an instance variable to hold a URLConnection

Our class contains a main () method to handle the logic flow of browsing a document, and our class also contains a getDocumentAt () method to connect to the server and request the document. Let's explore the details of these methods.

Browse the document

Public static void main (String [] args) {URLClient client = new URLClient (); String yahoo = client.getDocumentAt ("http://www.yahoo.com"); System.out.println (yahoo);}

Our main () method simply creates an instance of a new URLClient class and uses a valid URL String to call the getDocumentAt () method. When the call returns the document, we store it in a String and output the String to the console. However, the actual work is done in the getDocumentAt () method.

Request a document from the server

The getDocumentAt () method deals with how to get a document from web in practice:

Public String getDocumentAt (String urlString) {StringBuffer document = new StringBuffer (); try {URL url = new URL (urlString); URLConnection conn = url.openConnection (); BufferedReader reader = new BufferedReader (new InputStreamReader (conn.getInputStream (); String line = null;while ((line = reader.readLine ())! = null) document.append (line + "\ n"); reader.close ();} catch (MalformedURLException e) {System.out.println ("Unable to connect to URL:" + urlString) } catch (IOException e) {System.out.println ("IOException when connecting to URL:" + urlString);} return document.toString ();}

The getDocumentAt () method has a parameter of type String containing the URL of the document we want. Let's first create a line that StringBuffer uses to save the document. Next, we create a new URL with the parameter urlString passed in. Then, we create a URLConnection and open it:

URLConnection conn = url.openConnection ()

Once we have a URLConnection, we get its InputStream and wrap it as InputStreamReader, and then we wrap it as BufferedReader so that we can read the lines of the document we got from the server, which we often use when dealing with socket in java code. You must be familiar with it before we continue to study:

BufferedReader reader = new BufferedReader (new InputStreamReader (conn.getInputStream ()

With BufferedReader, we can easily read the contents of the document. We call the readline () method on reader in a while...loop loop:

String line = null

While (line = reader.readLine ())! = null)

Document.append (line + "\ n")

Blocking occurs when a line Terminator (such as a newline character) is passed from InputStream after the readLine () method is called. If not, it will continue to wait and return null when the connection is closed, in which case, once we get a line, we append it to the StringBuffer of a calling document with a newline character. This retains the format of the original document on the slave server.

When we have read all the lines, we should turn off BufferedReader:

Reader.close ()

If the URL constructor provided to urlString is invalid, a MalformedUR Tara LException exception will be thrown. Similarly, if other errors occur, such as getting InputStream from a connection, an IOException will be thrown.

Summary

1. Instantiate URL with a valid url String of the resource you want to connect to

2. Connect to the specified URL

3. Wrap InputStream to connect to BufferedReader so that you can read lines

4. Use your BufferedReader to read the contents of the document

5. Close BufferedReader

The above is how to use the following three classes in the Java.net package. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report