zoukankan      html  css  js  c++  java
  • RestTemplate中headers中添加Host不生效

    在使用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
  • 相关阅读:
    使用kubeadm部署K8S v1.17.0集群
    06Shell并发控制
    05Shell循环语句
    04Shell流程控制
    03Shell条件测试
    02Shell变量
    01Shell入门02-echo和printf
    01Shell入门01-bash Shell特性
    局域网部署ntp时间服务器
    聊聊、Mybatis集成Spring XML方式
  • 原文地址:https://www.cnblogs.com/exmyth/p/14791950.html
Copyright © 2011-2022 走看看