The Java platform implements socket in the java.net package .. Net packages mainly work by the following three classes:
· Urlconnection · socket · serversocket contains more classes in the java.net package, but these are the most common ones. Let's start with urlconnection, this class provides the method of using socket in your Java code without understanding the underlying mechanism of socket. You don't even need to try to connect to a URL using sockets. The following steps are involved: Create a urlconnection and configure it using different setter methods. Connect to urlconnection. interact with different getter methods, we will use some examples to demonstrate how to use urlconnection to request a document from a server. The urlclient class starts 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: you must first import java.net and Java. i/O package. Let's give our class an instance variable to save a urlconnection. Our class contains a main () method used to process the logic flow that browses a document ), our class also contains the getdocumentat () method used to connect to the server and request documentation. Below we will explore the details of these methods. The main () method is used to process the logic flow of a 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 only creates a new URLClient class instance and uses a valid URL String to call the getDocumentAt () method. When a document is returned, we store it in a String and output the String to the console. However, the actual work is completed in the getDocumentAt () method. Request a document from the server using the getDocumentAt () method. In actual work, how can I obtain a document from the web: 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 string type parameter that contains the URL of the document we want to get. First, create a stringbuffer to save the line of the document. Next, we use the passed parameter urlstring to create a new URL. Then, we create a urlconnection and open it: urlconnection conn = URL. openconnection (); once a urlconnection exists, we obtain its inputstream and package it into inputstreamreader, then we wrap it into bufferedreader so that we can read the lines of documents obtained from the server. This packaging technology is often used when processing sockets in Java code. Before proceeding, you must be familiar with it: bufferedreader reader = new bufferedreader (New inputstreamreader (conn. getinputstream (); with bufferedreader, we can easily read the content of the document. We call the Readline () method on reader in a while... loop: String line = NULL;
While (line = reader. Readline ())! = NULL)
Document. append (LINE + "/N"); blocking occurs only when the row terminator (for example, line feed) is passed in from inputstream after the Readline () method is called. If not, it will continue to wait. When the connection is closed, it will return NULL. In this case, once we get a row, we append it to the stringbuffer of a call document together with a linefeed. In this way, the format of the original document on the slave server is retained. After reading all rows, we should disable bufferedreader: reader. Close (). If the URL constructor provided to urlstring is invalid, A malformedur tralexception exception will be thrown. If other errors occur, for example, when inputstream is obtained from a connection, an ioexception is thrown. Conclusion 1. use a valid URL string of the resource you want to connect to instantiate URL 2. connect to the specified URL 3. wrap inputstream to connect to bufferedreader so that you can read row 4. use your bufferedreader to read the document content. disable bufferedreader