Implementation of HTTP protocol client application with socket class

Source: Internet
Author: User
Tags socket

HTTP client programs are integrated in the Java language and can be invoked through the URLConnection class. Unfortunately, the details of the implementation of the HTTP client program are still a mystery, as Sun does not disclose the source code for the app. In this paper, an HTTP protocol client program is implemented using the Java.net.Socket class according to the HTTP protocol specification.

1.Socket class:

Readers who understand the TCP/IP protocol set communication know that communication between protocols is done through sockets. In the java.net package, the socket class is the specific implementation of the socket. It returns an I/O stream and realizes the exchange of information between protocols by connecting to the host.

2. HTTP protocol

The HTTP protocol, like the Protocols in other TCP/IP protocol sets, follows client/server model work. The information that the client sends to the server is formatted as follows:

The version number of the request method URL HTTP protocol

Submitted meta-Information

* * Empty Line * *

Entity

The request method is a description of the connection work, and the HTTP protocol has been developed to version 1.1, which includes getting, head, POST, DELETE, OPTIONS, TRACE and put seven kinds. The meta information is information about the current request. Through the analysis of Meta information, you can check the integrity of the Entity data, receive process errors, type matching, and so on. The introduction of Meta information makes HTTP protocol communication more secure and reliable. The entity is the specific content of the request.

Send the above message to the Web server, and if successful, the answer format is as follows:

Version number answer status code description for HTTP protocol

Received meta-information

* * Empty Line * *

Entity

Send the report to the client, and receive success, close the connection between each other, complete a handshake. The following are the most commonly used get methods to illustrate specific message applications:

GET http://www.youhost.com HTTP/1.0
accept: www/source; text/html; image/gif; image/jpeg; */*
User_Agent: myAgent
**空行**

This message is to request a default HTML document to the Www.youhost.com host. The client HTTP protocol version number is 1.0, the meta information includes the file format that can be received, the user agent, each paragraph is separated by a carriage return newline character, and finally ends with a blank line. After sending to the server, if the execution process is normal, the server returns the following code:

http/1.1 OK

Date:tue, Sep 1999 02:19:57 GMT

server:apache/1.2.6

Connection:close

Content-type:text/html

* * Empty Line * *

http/1.1 that this HTTP server is version 1.1, 200 is the server's response status code requested by the client, OK is the answer status code explanation, then the document's meta information and document body. (For an explanation of the related answer status codes and meta information, see Inetrnet Standard Draft: RFC2616).

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.