java URL programming ( http get , post )
Connection steps:
1. Create a URL object
URL(String url)
2. Open the connection URLConnection through the URL object
URLConnection openConnection()
3. Obtain the input and output stream through the URLConnection object
InputStream =connection.getInputStream()
OutputStream =connection.getOutputStream()
What is the difference between Http get and Post?
get is used by the client to obtain information from the server, although parameters can be passed, but because the parameters need to follow the url, it is limited by the length of the url, and the parameters can be seen in the address bar
The post user passes a large number of parameters to the server, and the parameters are passed to the server in the form of the request body. In theory, there is no length limit
java TCP -based Socket programming
Server:
ServerSocket serversocket=new ServerSocket(int port) -- "Listening port, if it is 0, a free port will be automatically allocated
Socket socket=serversocket.accept(); -- "Get the connection, which happens after 3 handshakes
InputStream =socket.getInputStream();
OutputStream =socket.getOutputStream();
Client:
Socket socket=new Socket(String host,int port); -- "establish a connection
InputStream =socket.getInputStream();
OutputStream =socket.getOutputStream();
Difference between HTTP and Socket
Answer: 1. http is an application layer protocol built on the TCP protocol, and Socket supports different transport layer protocols TCP or UDP.
2. Socket can actively push messages to the client, HTTP must first request and then respond.
Introduction to HTTP Protocol ( Stateless )
Hypertext Transfer Protocol, stateless, using client server mode, HTTP process :
( 1 ) The client and the server establish a connection
( 2 ) The client sends a request to the server
( 3 ) The server sends a response
( 4 ) The client receives the server response information and displays it to the user through the browser,
The information is transmitted in plain text, using port 80 by default , and the ciphertext transmission of https information, using port 443 .
Insufficient HTTP :
1. Information transmission in plain text
2. Unable to verify the identity of the communicating party - disguised client, server
Stateless : Two consecutive requests for the same session are unaware of each other, and if subsequent processing requires previous information, it must be retransmitted.
Techniques for maintaining HTTP status : ( 1 ) Cookie --> Client
( 2 ) Session --> server side
Cookies are disabled : get method URL rewrite; post method submit hidden form
HTTP1.0 vs HTTP1.1 ?
Answer: (1) 1.0 non-persistent connection --> a connection can only send one HTTP request (Tcp short connection )
1.1 Support persistent connection --> A connection can send multiple HTTP requests (Tcp long connection )
There are two ways of persistent connection:
1. Non-pipeline mode : the client can only send the next request after receiving the response of the previous request
2. Pipeline method: The client can send the next request before receiving the response of the previous request, but the server must give the responses in the order of the requests.
(2) 1.1 added the cache-control response header to control the caching of information in the browser.
Heartbeat packet: used for keep-alive and disconnection processing of long connections .
Heartbeat detection steps : The
client sends a heartbeat packet to the server every fixed time , and starts a timeout counter. If it receives a response from the server within the timeout period, it means the server is normal, delete the timeout counter, and if it does not receive the server within the timeout period. In response, it is considered that the server hangs up and the client closes the connection. If the server cannot receive the heartbeat packet for a long time, it should also close the connection . When reconnecting for the first time, wait for the INTERVAL time to initiate a reconnection to ensure that the server has sufficient time to release handle resources.