Home Backend Development PHP Tutorial 判断Keep-Alive模式的HTTP请求的结束的实现代码_PHP

判断Keep-Alive模式的HTTP请求的结束的实现代码_PHP

Jun 01, 2016 pm 12:15 PM
http request keep-alive

所以根据EOF就可判断一次请求的结束,下面的代码(PHP)很常见:
复制代码 代码如下:
// $fp是由fsockopen()产生的句柄
while(!feof($fp)) {
echo fgets($fp);
}

(注:短连接模式是在头部用”Connection: close”标示,长连接用”Connection: keep-alive”标示。目前HTTP/1.0默认使用短连接,HTTP/1.1默认使用长连接。)
而长连接(也称持久连接)模式的HTTP在发送完数据后服务器并不断开连接,而是留着下一次HTTP请求时使用,所以长连接的好处是显而易见的,通过共用一个TCP连接来节省以后请求时建立/断开连接的开销。而EOF是直到这个TCP连接结束(超时或出错)时才会被发送,所以我们就不能使用上面的办法来判断一次HTTP请求的结束了。这也是使用长连接时都会遇到的一个问题。目前判断的方法主要有两种:
(1) 根据头中的Content-Length字段。这个字段标明了正文的长度,我们可以以接收完指定长度的字符为判断结束的依据。
(2) 在没有Content-Length时,根据Transfer-Encoding。有些时候服务器无法确定正文的大小,因为正文可能是动态产生的,所以就不会提供Content-Length了,而是采用chunk编码来一块一块地发送正文。每个chunk块由头部和正文两部分组成,头部中由一个16进制数字指定了正文的长度;最后由一个长度为0的chunk块来表示整个HTTP正文的结束。
下面我用PHP实现了有Content-Length时的判断方式:
1. 获得Content-Length值
复制代码 代码如下:
$length = 0;
$line = '';
while($line !== "\r\n") {
$line = fgets($fp);
if(substr($line, 0, 15) === 'Content-Length:') {
$length = intval(substr($line, 16));
}
}

2. 获得正文
复制代码 代码如下:
$sum = 0;
while($sum $line = fgets($fp);
$sum += strlen($line);
echo $line;
}

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

How to solve the problem of HTTP request connection refused in Java development How to solve the problem of HTTP request connection refused in Java development Jun 29, 2023 pm 02:29 PM

How to solve the problem of HTTP request connection being refused in Java development. In Java development, we often encounter the problem of HTTP request connection being refused. This problem may occur because the server side restricts access rights, or the network firewall blocks access to HTTP requests. Fixing this problem requires some adjustments to your code and environment. This article will introduce several common solutions. Check the network connection and server status. First, confirm that your network connection is normal. You can try to access other websites or services to see

Cause analysis: HTTP request error 504 gateway timeout Cause analysis: HTTP request error 504 gateway timeout Feb 19, 2024 pm 05:12 PM

Brief introduction to the reason for the http request error: 504GatewayTimeout: During network communication, the client interacts with the server by sending HTTP requests. However, sometimes we may encounter some error messages during the process of sending the request. One of them is the 504GatewayTimeout error. This article will explore the causes and solutions to this error. What is the 504GatewayTimeout error? GatewayTimeo

Solution: Socket Error when handling HTTP requests Solution: Socket Error when handling HTTP requests Feb 25, 2024 pm 09:24 PM

http request error: Solution to SocketError When making network requests, we often encounter various errors. One of the common problems is SocketError. This error is thrown when our application cannot establish a connection with the server. In this article, we will discuss some common causes and solutions of SocketError. First, we need to understand what Socket is. Socket is a communication protocol that allows applications to

Set query parameters for HTTP requests using Golang Set query parameters for HTTP requests using Golang Jun 02, 2024 pm 03:27 PM

To set query parameters for HTTP requests in Go, you can use the http.Request.URL.Query().Set() method, which accepts query parameter names and values ​​as parameters. Specific steps include: Create a new HTTP request. Use the Query().Set() method to set query parameters. Encode the request. Execute the request. Get the value of a query parameter (optional). Remove query parameters (optional).

How Nginx implements retry configuration of HTTP requests How Nginx implements retry configuration of HTTP requests Nov 08, 2023 pm 04:47 PM

How Nginx implements HTTP request retry configuration requires specific code examples. Nginx is a very popular open source reverse proxy server. It has powerful functions and flexible configuration options and can be used to implement HTTP request retry configuration. In network communication, sometimes the HTTP request we initiate may fail due to various reasons, such as network delay, server load, etc. In order to improve the reliability and stability of the application, we may need to retry when the request fails. The following will introduce how to use Ng

How to use Nginx for compression and decompression of HTTP requests How to use Nginx for compression and decompression of HTTP requests Aug 02, 2023 am 10:09 AM

How to use Nginx to compress and decompress HTTP requests Nginx is a high-performance web server and reverse proxy server that is powerful and flexible. When processing HTTP requests, you can use the gzip and gunzip modules provided by Nginx to compress and decompress the requests to reduce the amount of data transmission and improve the request response speed. This article will introduce the specific steps of how to use Nginx to compress and decompress HTTP requests, and provide corresponding code examples. Configure gzip module

Send HTTP request and handle response using HttpClient in Java 11 Send HTTP request and handle response using HttpClient in Java 11 Aug 01, 2023 am 11:48 AM

Title: Sending HTTP requests and handling responses using HttpClient in Java11 Introduction: In modern Internet applications, HTTP communication with other servers is a very common task. Java provides some built-in tools that can help us achieve this goal. The latest and recommended one is the HttpClient class introduced in Java11. This article will introduce how to use HttpClient in Java11 to send HTTP requests and process responses,

See all articles