In Java 11 HttpClient how to solve restricted header name: Date

安稳与你 提交于 2019-12-19 06:38:03

问题


The following java 11 code:

HttpRequest request = HttpRequest.newBuilder()
            .uri(uri)
            .header("Digest", digest)
            .header("Date", date)  
            .build();

gives the following error:

Exception in thread "main" java.lang.IllegalArgumentException: restricted header name: "Date"

The problem is that the digest is based on the date, so I cannot simply rely on the http client date, because that will make the digest invalid. I need a way to either set the Date header, or to retrieve the Date header and then set the digest thereafter. Neither seems possible with standard java 11.


回答1:


Bug#JDK-8213189

This is a bug registered in current JDK's http-client implementation where Date is categorized as a restricted header as well.

In JDK/12, there would be a provision for other such restricted headers to be allowed using the attribute:

-jdk.httpclient.allowRestrictedHeaders


来源:https://stackoverflow.com/questions/53979173/in-java-11-httpclient-how-to-solve-restricted-header-name-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!