java 域名为简单名称时,https 客户端不发送 cookie

java https client doesn't send cookies when domain is simple name

我有以下代码。

  public void setCookie(String asCookie) {
    host = applicationURL.getHost();
    if ((host != null) && (asCookie != null)) {
      HttpCookie tCookie = new HttpCookie(COOKIE_NAME, asCookie);
      tCookie.setDomain(host);
      tCookie.setSecure(true);
      tCookie.setPath("/");
      tCookie.setVersion(0);
      try {
        URI basicCookie = new URI(PREFERRED_PROTOCOL, host, "/");
        cookieJar.add(basicCookie, tCookie);
      } catch (URISyntaxException ex) {
        System.out.println("ERROR:SessionID.setCookie:invalid host: " + ex);
        ex.printStackTrace();
      }
    } else {
      System.out.println("ERROR:SessionID.setCookie: host or cookie is empty!");
    }
  }

当主持人在

注意:gigigigi.kent 并在 /etc/hosts 文件中指向上面的 ip 地址

我是运行 openjdk 1.8.0.282 Windows.

我怀疑没有点的域有问题,但我真的很难弄清楚这个问题,尤其是如果它与前一个一起工作的话。

我已经在 apache 中用 mod_log_forensic 跟踪了 header 并且在它不工作的情况下我没有得到任何 cookie。

工作

HTTP/1.1|connection:close|Content-Type:application/x-www-form-urlencoded|Cache-Control:no-cache|Pragma:no-cache|User-Agent:Java/1.8.0_282|Host:gigi.kent|Accept:text/html, image/gif, image/jpeg, *; q=.2, /; q=.2|Content-Length:20|Cookie:SESSION=5rnYovJ@ibNplzsD1P8

不工作

HTTP/1.1|connection:close|Content-Type:application/x-www-form-urlencoded|Cache-Control:no-cache|Pragma:no-cache|User-Agent:Java/1.8.0_282|Host:gigi|Accept:text/html, image/gif, image/jpeg, *; q=.2, /; q=.2|Content-Length:61

问题实际上在于创建 URI 的方式。使用以 https:///gigi/do/some/stuff 形式从应用程序收到的原始 URL 并通过 toURI() 获取 URI 是解决方案。

下面是差异。

       try {
-        URI basicCookie = new URI(PREFERRED_PROTOCOL, host, "/");
+        URI basicCookie = applicationURL.toURI();
         cookieJar.add(basicCookie, tCookie);

所以最终的代码如下:

  try {
    URI basicCookie = applicationURL.toURI();
    cookieJar.add(basicCookie, tCookie);
  } catch (URISyntaxException ex) {