在使用restTemplate访问内网接口时,不打算指host,支持ip访问,所以我们需要再header中指定host.但经调试,发现HttpURLConnection中Host无法覆盖.
解决方案:

Object requestBody = ImmutableMap.of("username", username, "password", password);
HttpEntity request = requestWithHeader(requestBody, headers -> {
headers.add(HttpHeaders.HOST,DEFAULT_API_ACCOUNT_DOMAIN);
headers.add(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE);
});

//重写HttpURLConnection的headers属性Host,否则自定义添加Host无法覆盖.
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");

 

https://stackoverflow.com/questions/43223261/setting-host-header-for-spring-resttemplate-doesnt-work
 
https://stackoverflow.com/questions/7648872/can-i-override-the-host-header-where-using-javas-httpurlconnection-class 
 
https://stackoverflow.com/questions/9096987/how-to-overwrite-http-header-host-in-a-httpurlconnection
 
 
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
相关资源
相似解决方案