Home  >  Article  >  Java  >  Detailed explanation of Java and HTTP protocol

Detailed explanation of Java and HTTP protocol

黄舟
黄舟Original
2017-09-08 09:45:451617browse

This article mainly introduces relevant information about the detailed introduction of Java and Http protocols. Here are examples to help you learn and understand this part of the content. Friends in need can refer to

Java and Http Detailed introduction to the protocol

Introduction                                             , often based on TCP connection method. The main features of the HTTP protocol are: 1. Support client/server mode.

2. Simple and fast: When a client requests a service from the server, it only needs to send the request method and path. Because the HTTP protocol is simple, the communication speed is very fast.

3. Flexible: HTTP allows the transmission of any type of data object. Types are marked by Content-Type.
4. No connection: that is, each connection only handles one request. After processing the customer's request and receiving the customer's response, the connection is disconnected. This method saves transmission time.
5. Stateless: Stateless means that the protocol has no memory ability for transaction processing.

The default connection method of http1.0 protocol is non-persistent connection, and the default connection method of HTTP1.1 is persistent connection.

Non-persistent connection

: Every time the server issues an object, the corresponding TCP connection is closed, which means that each connection does not last long enough to be used to transmit other objects. Each TCP connection is only used to transmit one request message and one response message.

Persistent Connection

: The server keeps the TCP connection open after sending a response. Subsequent requests and responses between the same client/server pair can be sent over this connection. The default mode of HTTP/1.1 uses pipelined persistent connections.

1. Request for detailed explanation of HTTP protocol

//请求行
POST /reg.jsp HTTP/ (CRLF)    

//消息报头
Accept:image/gif,image/x-xbitmap,image/jpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/* (CRLF)
Accept-Language:zh-cn (CRLF)
Accept-Encoding:gzip,deflate (CRLF)
If-Modified-Since:Wed,05 Jan 2007 11:21:25 GMT (CRLF)
If-None-Match:W/"80b1a4c018f3c41:8317" (CRLF)
User-Agent:Mozilla/4.0(compatible;MSIE6.0;Windows NT 5.0) (CRLF)
Host:www.guet.edu.cn (CRLF)
Connection:Keep-Alive (CRLF)
(CRLF)

//请求正文
user=jeffrey&pwd=1234

The above are the three parts of http request:
Request line, message Header, request body.

The request line starts with a method symbol, separated by spaces, followed by the requested URI and protocol version, in the following format:

Method Request-URI HTTP-Version CRLF

Where Method represents the request method (such as POST, GET, PUT, DELETE, etc.); Request-URI is a uniform resource identifier; HTTP-Version represents the requested HTTP protocol version; CRLF represents carriage return and line feed.


2. Response Chapter Detailed Explanation of HTTP Protocol

//状态行
HTTP/1.1 200 OK (CRLF)

//消息报头
Cache-Control: private, max-age=30
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Mon, 25 May 2009 03:20:33 GMT
Last-Modified: Mon, 25 May 2009 03:20:03 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Mon, 25 May 2009 03:20:02 GMT
Content-Length: 12173

//响应正文
略

HTTP response is also composed of three parts, namely:
Status line, message header, response body

The status line format is as follows:


##

  HTTP-Version Status-Code Reason-Phrase CRLF

Among them, HTTP-Version represents server HTTP The version of the protocol; Status-Code represents the response status code sent back by the server; Reason-Phrase represents the text description of the status code.


Common status codes, status descriptions, instructions:

200 OK //The client request is successful

400 Bad Request //The client request has a syntax error and cannot be accepted by the server Understood
401 Unauthorized //The request is not authorized, this status code must be used together with the WWW-Authenticate header field

403 Forbidden //The server received the request, but refused to provide the service

404 Not Found // The requested resource does not exist, eg: the wrong URL was entered
500 Internal Server Error //An unexpected error occurred in the server
503 Server Unavailable //The server is currently unable to process the client's request and may return to normal after a period of time


3. Detailed explanation of the message header of the HTTP protocol

The HTTP message consists of a request from the client to the server and a response from the server to the client. Both request messages and response messages consist of a start line (for a request message, the start line is the request line; for a response message, the start line is the status line), message header (optional), blank line (only CRLF line), message body (optional) composition. HTTP message headers include ordinary headers, request headers, response headers, and entity headers. Each header field is composed of name + ":" + space + value. The name of the message header field is case-independent.

1. Request header

The request header allows the client to transmit additional information of the request and the client's own information to the server.

Commonly used request headers

Accept请求报头域用于指定客户端接受哪些类型的信息。
Accept-Charset请求报头域用于指定客户端接受的字符集。
Accept-Encoding请求报头域类似于Accept,但是它是用于指定可接受的内容编码。
Accept-Language请求报头域类似于Accept,但是它是用于指定一种自然语言。
Authorization请求报头域主要用于证明客户端有权查看某个资源。

Host请求报头域主要用于指定被请求资源的Internet主机和端口号,它通常从HTTP URL中提取出来的。User-Agent请求报头域允许客户端将它的操作系统、浏览器和其它属性告诉服务器。

2、响应报头

     响应报头允许服务器传递不能放在状态行中的附加响应信息,以及关于服务器的信息和对Request-URI所标识的资源进行下一步访问的信息。

常用的响应报头

Location响应报头域用于重定向接受者到一个新的位置。Location响应报头域常用在更换域名的时候。
Server响应报头域包含了服务器用来处理请求的软件信息

3. 实体报头

请求和响应消息都可以传送一个实体。

常用的实体报头

Content-Encoding指示已经被应用到实体正文的附加内容的编码。

Content-Language实体报头域描述了资源所用的自然语言。

Content-Length实体报头域用于指明实体正文的长度,以字节方式存储的十进制数字来表示。
Content-Type实体报头域用语指明发送给接收者的实体正文的媒体类型。
Last-Modified实体报头域用于指示资源的最后修改日期和时间。
Expires实体报头域给出响应过期的日期和时间。

 四、补充

1、HTTP协议Content Lenth限制漏洞导致拒绝服务攻击

使用POST方法时,可以设置ContentLenth来定义需要传送的数据长度,例如ContentLenth:999999999,在传送完成前,内 存不会释放,攻击者可以利用这个缺陷,连续向WEB服务器发送垃圾数据直至WEB服务器内存耗尽。这种攻击方法基本不会留下痕迹。

2、为了提高用户使用浏览器时的性能,现代浏览器还支持并发的访问方式,浏览一个网页时同时建立多个连接,以迅速获得一个网页上的多个图标,这样能更快速完成整个网页的传输。HTTP1.1中提供了这种持续连接的方式,而下一代HTTP协议:HTTP-NG更增加了有关会话控制、丰富的内容协商等方式的支持,来提供更高效率的连接。

 五.Java利用HTTP协议实现联网和下载

Url的请求连接(Get方式)


String currentUrl=“http://www.myWeb.com/login.jsp?userName='Devin'&passWord='mypassword'”; //URL ?后面的内容为HTTP请求的正文
URL url = new URL(currentUrl);
 
HttpURLConnection httpurlconnection = url.openConnection();
//下面的设置对应HTTP请求中的消息报头
httpurlconnection.setRequestProperty("User-Agent",CommonValues.User_Agent);
httpurlconnection.setRequestProperty("Accept",CommonValues.Accept);
httpurlconnection.setRequestProperty("Accept-Charset",CommonValues.Accept_Charset);
httpurlconnection.setRequestProperty("Accept-Language",CommonValues.Accept_Language);
httpurlconnection.setRequestProperty("Connection",CommonValues.Connection);
httpurlconnection.setRequestProperty("Keep-Alive",CommonValues.Keep_Alive);
httpurlconnection.setConnectTimeout(CommonValues.ConnectionTimeOut);
httpurlconnection.setReadTimeout(CommonValues.ReadTimeOut);
       
httpurlconnection.connect();
      
int responsecode = httpurlconnection.getResponseCode();
       
if(responsecode == HttpURLConnection.HTTP_OK) //对应HTTP响应中状态行的响应码
{
  //操作请求流,这里对应HTTP响应中的响应正文
}
       
if (httpurlconnection != null) 
{
  httpurlconnection.disconnect();
}

The above is the detailed content of Detailed explanation of Java and HTTP protocol. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn