![](/img/trans.png)
[英]How to print all the request headers which HttpURLConnection is sending?
[英]HttpURLConnection is not returning all headers
我正在使用以下代碼打印HTTP標頭。
URL url = new Url("htttp://example.com/example");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() +
" ,Value : " + entry.getValue());
}
這是輸出:
Key : null ,Value : [HTTP/1.1 200 OK]
Key : Accept-Ranges ,Value : [bytes]
Key : Cache-Control ,Value : [max-age=604800, public]
Key : Connection ,Value : [Keep-Alive]
Key : Date ,Value : [Mon, 03 Oct 2016 18:01:06 GMT]
Key : ETag ,Value : ["159eb4-53dce1f957880-gzip"]
Key : Expires ,Value : [Mon, 10 Oct 2016 18:01:06 GMT]
Key : Keep-Alive ,Value : [timeout=5, max=100]
Key : Last-Modified ,Value : [Sat, 01 Oct 2016 13:59:46 GMT]
Key : Server ,Value : [Apache/2.4.12 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4]
Key : Transfer-Encoding ,Value : [chunked]
Key : Vary ,Value : [Accept-Encoding,User-Agent]
現在我正在嘗試通過curl:
$ curl -v -D - http://example.com/example -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 111.111.111.111...
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0* Connected to example.com (111.111.111.111) port 80 (#0)
> GET example.com/example HTTP/1.1
> Host: example.com
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Mon, 03 Oct 2016 18:25:11 GMT
Date: Mon, 03 Oct 2016 18:25:11 GMT
< Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
< Last-Modified: Sat, 01 Oct 2016 13:59:46 GMT
Last-Modified: Sat, 01 Oct 2016 13:59:46 GMT
< ETag: "159eb4-53dce1f957880"
ETag: "159eb4-53dce1f957880"
< Accept-Ranges: bytes
Accept-Ranges: bytes
< Content-Length: 1416884
Content-Length: 1416884
< Cache-Control: max-age=604800, public
Cache-Control: max-age=604800, public
< Expires: Mon, 10 Oct 2016 18:25:11 GMT
Expires: Mon, 10 Oct 2016 18:25:11 GMT
< Vary: Accept-Encoding,User-Agent
Vary: Accept-Encoding,User-Agent
<
{ [1080 bytes data]
30 1383k 30 427k 0 0 116k 0 0:00:11 0:00:03 0:00:08 116k^C
看看java輸出中缺少Content-Length
標頭的方式? 為什么會這樣? 我怎樣才能解決這個問題?
Content-Length
缺失,因為服務器以塊的形式發送響應。 這是預期的行為。 查看Java響應中的Transfer-Encoding
標頭,這里是RFC 2616,4.4 Message Length (參見列表的第3項)。
“Content-Length”自動解析,可以通過conn.getContentLength()
或conn.getContentLengthLong()
來檢索。 有關詳細信息,請參閱https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getContentLengthLong() 。
在java的情況下的響應包括'transfer-encoding'標頭,其在卷曲響應的情況下丟失。 此外,當我嘗試它時,我得到了404(未找到)我的嘗試。 當我從瀏覽器中嘗試時,我得到了404。
我的猜測是你有一個覆蓋了example.com
主機條目,它會循環回你自己的tomcat或者類似的東西。 這是您的環境問題。
這就是我得到的:
/**
*
* <P> jdk-1.8 </P>
* <PRE>
Key :null || Values :[HTTP/1.1 404 Not Found]
Key :X-Cache || Values :[HIT]
Key :Server || Values :[ECS (ewr/1445)]
Key :Etag || Values :["359670651+gzip+ident"]
Key :Cache-Control || Values :[max-age=604800]
Key :x-ec-custom-error || Values :[1]
Key :Vary || Values :[Accept-Encoding]
Key :Last-Modified || Values :[Fri, 09 Aug 2013 23:54:35 GMT]
Key :Expires || Values :[Mon, 10 Oct 2016 19:20:29 GMT]
Key :Content-Length || Values :[1270]
Key :Date || Values :[Mon, 03 Oct 2016 19:20:29 GMT]
Key :Content-Type || Values :[text/html]
* </PRE>
* @param urlStr (http://example.com/example)
*/
private static void printHeaders(String urlStr) {
try {
URL urlRef = new URL(urlStr);
URLConnection urlConnection = urlRef.openConnection();
Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
for (String headerName : headerFields.keySet()) {
List<String> headerEntry = headerFields.get(headerName);
System.out.println("Key :"+headerName +" || Values :"+headerEntry);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.