这是我用来获取内容编码的代码,但是似乎yahoo和google等网站没有使用任何内容编码。我的意思是我的代码错误或者实际上他们没有使用内容编码,我似乎认为是前者。所以我只想知道哪里出了问题?
package com.java24hours;
import java.io.*;
import java.net.*;
import java.net.http.*;
import java.util.*;
public class ServerCheck{
static String[] sites = {"https://www.google.com","https://www.yahoo.com"};
public static void check() throws URISyntaxException,IOException,InterruptedException{
for(String site: sites){
System.out.println("\nSite: " + site);
HttpClient browser = HttpClient.newHttpClient();
URI uri = new URI(site);
HttpRequest request = HttpRequest.newBuilder(uri).build();
HttpResponse<String> response = browser.send(request,HttpResponse.BodyHandlers.ofString());
Optional<String> contenttype = response.headers().firstValue("Content-Type");
Optional<String> contentencoding = response.headers().firstValue("Content-Encoding");
if (contentencoding.isPresent()){
System.out.println("Content-Encoding: " + contentencoding.get());
}else{
System.out.println("Content-Encoding not found");
}
if (contenttype.isPresent()){
System.out.println("Content-Type: " + contenttype.get());
}else{
System.out.println("Content-Type not found");
}
}
}
public static void main(String[] args) throws URISyntaxException,IOException,InterruptedException{
ServerCheck.check();
}
}
答案 0 :(得分:2)
Content-Encoding
仅在客户端允许时使用。
由于您没有在请求中发送标头Accept-Encoding
,这意味着仅允许identity
编码,并且由于这是默认编码,因此在其中没有Content-Encoding
标头响应。
如果要压缩,请指定标题:
HttpRequest request = HttpRequest.newBuilder(uri)
.setHeader("Accept-Encoding", "gzip")
.build();