如何在Java中进行HTTP请求并忽略正文

huangapple go评论87阅读模式
英文:

How to make an HTTP request in Java and ignore the body

问题

更具体地说,我指的是仅在网络上传输HTTP头部并在客户端接收响应正文之前停止通信。

示例

客户端发出请求

GET / HTTP/1.1
Host: example.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (Java/1.8.0_262)
Accept-Encoding: gzip,deflate

然后在网络上传输的响应只是

HTTP/1.1 200 OK
Date: Wed, 23 Sep 2020 22:41:21 GMT
Server: Apache
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: public, max-age=10800
Content-Language: en
Vary: Cookie,Accept-Encoding
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Age: 1865
grace: none
Content-Length: 9213
Connection: keep-alive
Accept-Ranges: bytes
英文:

To be more specific, I mean specifically to just consume the HTTP headers over the network and stop the communication before the client receives the response body.

Example

Client makes a request

GET / HTTP/1.1
Host: example.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (Java/1.8.0_262)
Accept-Encoding: gzip,deflate

Then the response over the network is just

HTTP/1.1 200 OK
Date: Wed, 23 Sep 2020 22:41:21 GMT
Server: Apache
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: public, max-age=10800
Content-Language: en
Vary: Cookie,Accept-Encoding
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Age: 1865
grace: none
Content-Length: 9213
Connection: keep-alive
Accept-Ranges: bytes

答案1

得分: 1

Http协议有六种方法,其中一种方法是'HEAD'。您可以尝试使用HEAD方法而不是GET方法。

另一种愚蠢的方法是:声明一个网络接口,然后返回空字符串。就像这样:

// 一个网络接口
String result = "";
return result;
英文:

Http protocol has six method, one of the methods is 'HEAD'. You can try use HEAD method instead of GET method.

And another stupid way : declare a web interface, and return null string.Like this:

// a web interface
String result = "";
return result;

huangapple
  • 本文由 发表于 2020年9月24日 07:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64037580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定