Web Service working mechanism based on Jave (2)

Source: Internet
Author: User
Tags header http request socket port number stringbuffer
HTTP Response (responses)

Like the request, an HTTP response also contains three parts:

Protocol Status Code description (Protocol-status code-description)
Response header (Response headers)
Entity (Entity body)
The following is a simple example of an HTTP response:

http/1.1 OK
server:microsoft-iis/4.0
Date:mon, 3 1998 13:13:33 GMT
Content-type:text/html
Last-modified:mon, 1998 13:23:42 GMT
content-length:112

<title>http Response example</title>Welcome to Brainy Software
</body>

The response header in the first line is very similar to the request header above. The first line tells us that the protocol is using the HTTP1.1, the response request has been successful (200 means successful), everything is OK.

The response header is similar to the request header and contains some useful information. The entity of the response is the content of the HTML section. The Baotou and the entities are also separated by the CRLF sequence.

  Socket class

A socket (socket) is an endpoint of a network connection. It enables the application to read and write through the network. By sending and receiving byte streams on a connection, two software programs located on different computers can communicate with each other. To send a message to another program, you need to know the IP address and the socket port number of the other machine. In Java, a socket is represented by the Java.net.Socket class.

To create a socket, you can do so using the constructor of the socket class. These constructors accept host names and ports:

Public Socket (String host, int port)
Host represents the remote computer name or IP address, port represents the port number for the remote application. For example, to connect to yahoo.com on port 80, you need to construct the following socket:

New Socket ("yahoo.com", 80);
Once you have successfully created an instance of the socket class, you can use it to send and receive byte streams. To send a byte stream, you must first call the Getoutputstream method of the socket class to obtain a Java.io.OutputStream object. To send a text to a remote application, you often construct a Java.io.PrintWriter object that is returned from the OutputStream object. To receive a byte stream at the other end of the connection, you call the getInputStream method of the socket class, which is returned from Java.io.InputStream.

The following program segment creates a socket, communicates with the local HTTP server (127.0.0.1 on behalf of the local), sends an HTTP request, and then receives a response from the server. It creates a stringbuffer to save the response and prints it to the console.

Socket socket = new Socket ("127.0.0.1", "8080");
OutputStream OS = Socket.getoutputstream ();
Boolean autoflush = true;
PrintWriter out = new PrintWriter (Socket.getoutputstream (), AutoFlush);
BufferedReader in = new BufferedReader (
New InputStreamReader (Socket.getinputstream ()));

Send an HTTP request to the Web server
Out.println ("get/index.jsp http/1.1");
Out.println ("host:localhost:8080");
Out.println ("Connection:close");
Out.println ();

Read the response
Boolean loop = true;
StringBuffer sb = new StringBuffer (8096);

while (loop) {
if (In.ready ()) {
int i=0;
while (I!=-1) {
i = In.read ();
Sb.append ((char) i);
}
loop = false;
}
Thread.CurrentThread (). Sleep (50);
}

Display the response to the out console
System.out.println (Sb.tostring ());
Socket.close ();

To get an exact response from the server, you need to send an HTTP request that follows the HTTP protocol rule. If you read the "Hypertext Transfer Protocol (HTTP)" Above, you should be able to understand the code that just created the socket above.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.