Http服务器,我什么时候发送数据?

时间:2011-06-15 12:17:23

标签: android http http-headers

我用Java构建了一个文件服务器,我很难理解何时应该开始提供文件数据。您可以在下面看到两个不同的客户端请求。有些提供了两个请求,我不确定何时应该开始提供数据以及何时应该“忽略”。

客户#1:

06-15 08:06:21.290: VERBOSE/HttpSession(9425): SESSION 1: STARTED
06-15 08:06:21.290: INFO/HttpSession(9425): CLIENT: 
06-15 08:06:21.290: INFO/HttpSession(9425): GET /stream HTTP/1.1
06-15 08:06:21.290: INFO/HttpSession(9425): Host: 127.0.0.1:37914
06-15 08:06:21.290: INFO/HttpSession(9425): User-Agent: (Linux)
06-15 08:06:21.290: INFO/HttpSession(9425): SERVER:
06-15 08:06:21.290: INFO/HttpSession(9425): HTTP/1.1 200 OK 
06-15 08:06:21.290: INFO/HttpSession(9425): Date: Wed, 15 Jun 2011 12:06:21 GMT
06-15 08:06:21.290: INFO/HttpSession(9425): Server: HTTP Server
06-15 08:06:21.290: INFO/HttpSession(9425): Last-Modified: Mon, 06 Aug 2009 01:02:23 GMT
06-15 08:06:21.290: INFO/HttpSession(9425): Accept-Ranges: none
06-15 08:06:21.290: INFO/HttpSession(9425): Content-Type: audio/mpeg 
06-15 08:06:21.290: INFO/HttpSession(9425): 
06-15 08:06:26.720: VERBOSE/HttpSession(9425): SESSION 2: STARTED
06-15 08:06:26.730: INFO/HttpSession(9425): CLIENT: 
06-15 08:06:26.730: INFO/HttpSession(9425): GET /stream HTTP/1.1
06-15 08:06:26.730: INFO/HttpSession(9425): Host: 127.0.0.1:37914
06-15 08:06:26.730: INFO/HttpSession(9425): Accept: */*
06-15 08:06:26.730: INFO/HttpSession(9425): SERVER:
06-15 08:06:26.730: INFO/HttpSession(9425): HTTP/1.1 200 OK 
06-15 08:06:26.730: INFO/HttpSession(9425): Date: Wed, 15 Jun 2011 12:06:26 GMT
06-15 08:06:26.730: INFO/HttpSession(9425): Server: HTTP Server
06-15 08:06:26.730: INFO/HttpSession(9425): Last-Modified: Mon, 06 Aug 2009 01:02:23 GMT
06-15 08:06:26.730: INFO/HttpSession(9425): Accept-Ranges: none
06-15 08:06:26.730: INFO/HttpSession(9425): Content-Type: audio/mpeg 
06-15 08:06:26.730: INFO/HttpSession(9425): 
06-15 08:06:26.730: VERBOSE/HttpSession(9425): SESSION 2: ENDED

客户#2:

06-15 05:03:58.079 I/HttpSession(18335): CLIENT: 
06-15 05:03:58.079 I/HttpSession(18335): GET /stream HTTP/1.1
06-15 05:03:58.079 I/HttpSession(18335): Host: 127.0.0.1
06-15 05:03:58.079 I/HttpSession(18335): Accept: */*
06-15 05:03:58.079 I/HttpSession(18335): Icy-MetaData:1
06-15 05:03:58.079 I/HttpSession(18335): User-Agent: QuickTime;NvMM HTTP Client v0.1
06-15 05:03:58.089 I/HttpSession(18335): SERVER:
06-15 05:03:58.089 I/HttpSession(18335): HTTP/1.1 200 OK 
06-15 05:03:58.089 I/HttpSession(18335): Date: Wed, 15 Jun 2011 09:03:58 GMT
06-15 05:03:58.089 I/HttpSession(18335): Server: HTTP Server
06-15 05:03:58.089 I/HttpSession(18335): Last-Modified: Mon, 06 Aug 2009 01:02:23 GMT
06-15 05:03:58.089 I/HttpSession(18335): Accept-Ranges: bytes
06-15 05:03:58.089 I/HttpSession(18335): Content-Type: audio/mpeg 

1 个答案:

答案 0 :(得分:0)

如果您尝试提供音频或视频,则客户端可能会尝试使用HTTP's Range header。 Range标头允许跳到音频/视频流中间,擦洗等功能。桌面浏览器通常会发出一些请求,一个用于发现是否支持Range标头,还有一个实际开始流内容的请求。只要您的服务器可以处理客户端终止套接字连接,您就可以安全地为每个连接启动流内容。

如果您感兴趣,可以查看Brisket - 这是一个基于Java的轻量级HTTP服务器,支持Range标头。