Network Programming (URL-urlconnection)
1. Class URL (java.net package)
1) a URL class represents a unified resource identifier, which is a pointer to the Internet "resource. Resources can be simple files or directories,
It can also be a reference to more complex objects, such as queries for databases or search engines.
Generally, a URL can be divided into several parts. The preceding URL example indicates that the protocol used is HTTP (Hypertext Transfer Protocol)
The exact meaning of this name on the host depends on the Protocol and host. This information is generally stored in files,
But it can be generated at any time. This part of the URL is called the path part.
You can specify a port for the URL. It is the port number used to establish a TCP connection to the remote host. If this port number is not specified,
Use the default port of the Protocol. For example, the default port of HTTP is 80.
2) constructor Summary
URL (string spec)
Create a URL object based on the string representation.
URL (string protocol, string host, int port, string file)
Creates a URL object based on the specified protocol, host, port number, and file.
URL (string protocol, string host, int port, string file, urlstreamhandler handler)
Creates a URL object based on the specified protocol, host, port number, file, and handler.
URL (string protocol, string host, string file)
Creates a URL Based on the specified protocol name, host name, and file name.
3) method summary
Int getdefaultport ()
Obtain the default port number associated with the URL protocol.
String GetFile ()
Get the file name of this URL.
String gethost ()
Obtain the Host Name of the URL (if applicable ).
String getpath ()
Obtain the path of the URL.
Int getport ()
Obtain the port number of the URL.
String getprotocol ()
Obtain the protocol name of the URL.
String getquery ()
Obtain the query part of this URL.
Urlconnection openconnection ()
Returns a urlconnection object, indicating the connection to the remote object referenced by the URL.
Inputstream openstream ()
Open the connection to this URL and return an inputstream used to read from the connection.
Note:
Openstream ():
Public final inputstream openstream ()
Throws ioexception
Open the connection to this URL and return an inputstream used to read from the connection. This method is short for the following method:
Openconnection (). getinputstream ()
Import java.net .*;
Class urldemo
{
Public static void main (string [] ARGs) throws malformedurlexception
{
URL url = new URL ("http: // 192.168.1.254/myweb/demo.html? Name = Haha & age = 30 ");
System. Out. println ("getprotocol ():" + URL. getprotocol ());
System. Out. println ("gethost ():" + URL. gethost ());
System. Out. println ("getport ():" + URL. getport ());
System. Out. println ("getpath ():" + URL. getpath ());
System. Out. println ("GetFile ():" + URL. GetFile ());
System. Out. println ("getquery ():" + URL. getquery ());
/* Int Port = getport ();
If (Port =-1)
Port = 80;
Getport () =-1
*/
}
}
2. urlconnection (java.net package)
Directly known subclass:
Httpurlconnection, jarurlconnection
1) The abstract class urlconnection is a super class of all classes. It represents the communication link between the application and the URL.
Such instances can be used to read and write resources referenced by this URL. Generally, it takes several steps to create a connection to a URL:
<1> create a connection object by calling the openconnection method on the URL.
<2> processing setting parameters and general request attributes.
<3> use the connect method to establish the actual connection to the remote object.
<4> remote objects become available. The header field and content of the remote object become accessible.
Use the following methods to modify the parameters:
Setallowuserinteraction
Setdoinput
Setdooutput
Setifmodifiedsince
Setusecaches
Use the following methods to modify general request attributes:
Setrequestproperty
You can use setdefaultallowuserinteraction and setdefaultusecaches to set the default values of the allowuserinteraction and usecaches parameters.
Each set method above has a get method used to obtain the parameter value or the common request attribute value. The applicable parameters and general request attributes depend on the protocol.
After establishing a connection to a remote object, you can use the following methods to access header fields and content:
Getcontent
Getheaderfield
Getinputstream
Getoutputstream
Some header fields must be frequently accessed. Follow these steps:
Getcontentencoding
Getcontentlength
Getcontenttype
Getdate
Getexpiration
Getlastmodifed
Provides convenient access to these fields. The getcontent method uses the getcontenttype Method to Determine the remote object type. It is easy for the subclass to override the getcontenttype method.
In general, all pre-connection parameters and general request attributes can be ignored: Pre-connection parameters and general request attributes are sensitive values by default. For most clients of this interface, there are only two required methods: getinputstream and getcontent, Which mirror to the URL class through convenient methods.
2) constructor Summary
Protected urlconnection (URL)
Construct a URL Connection to the specified URL.
3) method summary
Void addrequestproperty (string key, string value)
Adds the general request attribute specified by the key-value pair.
Abstract void connect ()
Open the communication link of the resource referenced by this URL (if such a connection has not been established ).
Long getifmodifiedsince ()
Returns the value of the ifmodifiedsince field of this object.
Inputstream getinputstream ()
Returns the input stream read from the opened connection.
Long getlastmodified ()
Returns the value of the last-modified header field.
Outputstream getoutputstream ()
Returns the output stream written to this connection.
Object getcontent ()
Obtain the URL Connection content.
Int getcontentlength ()
Returns the value of the Content-Length header field.
String getcontenttype ()
Returns the value of the Content-Type header field.
Long getdate ()
Returns the value of the date header field.
4. Note:
1) The urlconnection class can be split (data is split using the application layer protocol) to parse the header field data sent by the server, and only read the body.
The response header is no longer displayed. It encapsulates the HTTP protocol and socket object, so the data processing occurs at the application layer.
2) exception:
Exceptions not reported java.net. malformedurlexception; must be captured or
Declaration to throw
URL url = new URL ("http: // 192.168.0.2: 8080/html/xuyuan.html? Name
= Haha & age = 25 ");
Import java.net .*;
Import java. Io .*;
Class urlconnectiondemo
{
Public static void main (string [] ARGs) throws exception
{
URL url = new URL ("http: // 192.168.1.254: 8080/myweb/demo.html ");
Urlconnection conn = URL. openconnection ();
System. Out. println (conn );
Inputstream in = conn. getinputstream ();
Byte [] Buf = new byte [1, 1024];
Int Len = in. Read (BUF );
System. Out. println (new string (BUF, 0, Len ));
}
}
Network Programming
1. Socket:
1) constructor Summary
Socket ()
Use socketimpl of the default system type to create an unconnected socket
2) required methods:
Void connect (socketaddress endpoint)
Connect the socket to the server.
2. socketaddress (java.net)
This class indicates the socket address without any protocol attachment. As an abstract class, a subclass should be created for it through specific protocol-related implementations.
It provides immutable objects for sockets to be used for binding, connection, or return values.
3. socketaddress (java.net) ------> inetsocketaddress class
1) this type of implementation IP socket address (IP address + port number ). It can also be a pair (host name + port number). In this case,
Will try to resolve the host name. If the resolution fails, the address is considered as an unresolved address, but it can still be used in some situations,
For example, you can connect through a proxy.
It provides immutable objects for sockets to be used for binding, connection, or return values.
A wildcard is a special local IP address. It usually indicates "any" and can only be used for BIND operations.
2) constructor Summary
Inetsocketaddress (inetaddress ADDR, int port)
Create a socket address based on the IP address and port number.
4. In serversocket:
Constructor Summary
Serversocket (INT port, int backlog)
Use the specified backlog to create a server socket and bind it to the specified local port number.
Parameters:
Port-the specified port, or 0, indicates that any idle port is used.
Maximum length of the backlog queue. That is, the maximum number of users simultaneously accessible to the host.
07-network programming (domain name resolution)
1. when accessing the host, you first find the local (that is, the IP ing file of disk C), and the local is not found outside.
If it is found locally, the local address will be sent to the browser.
2. Benefits of configuring ing relationships:
1) configure the website to be accessed to the hosts file, which greatly improves the access speed. Because it reduces the access parsing distance.
For example:
#127.0.0.1 www.163.com
2) When some paid software is used, it will be automatically updated to the website, which will affect the use of the updated website host name in the hosts file for configuration.
The website will not be updated because it uses a local address, so the update fails. For example: #127.0.0.1 www.myeclipse.com (website update example) L
3) If a malicious website is configured in the hosts file, the malicious website will be blocked.
3. Local Connection> Internet Protocol (TCP/IP) ---> double-click to configure DNS
By default, telecom communication is the resolution server of China Telecom.
Ing between IP address and Host Name: c: \ windows \ system32 \ drivers \ etc