我要调用的目标API需要一个值为gzip
的accept-encoding头,它才能返回gzipped
响应。我尝试了不同的方法,但无法让Google http
客户端发送accept-encoding标头。我在这里想念什么?
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(
(HttpRequest request) -> {
request.setParser(new JsonObjectParser(JSON_FACTORY));
});
GenericUrl genericUrl = new GenericUrl(url);
try {
HttpRequest request = requestFactory
.buildGetRequest(genericUrl);
request.getHeaders().setAcceptEncoding("gzip");
HttpResponse response = request.execute();
} catch (IOException e) {
log.error("Unable to Get to URL: {}, error: {}",url, e.getMessage());
}
我希望get请求包含标头,但是如果我查看我的代理日志,则唯一发送的标头是:
用户代理: Google-HTTP-Java-Client/1.30.0 (gzip)
接受: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
连接: keep-alive
主持人:
我还尝试使用以下手动方式:
request.getHeaders().set("Accept-Encoding", "gzip");
request.getHeaders().set("Accept-Encoding2", "gzip");
这将导致成功发送accept-encoding2标头,但不会成功发送accept-encoding标头。