title: Setting a session cookie with the Java 11 HttpClient
body: <p>I'm trying to GET the content of a page using the Java 11 HttpClient. Since the page uses OAuth, I want to authenticate using my session cookie. I currently use the following code, inspired by <a href="https://stackoverflow.com/questions/15019642/trying-setting-session-cookie-using-httpclient">this question</a>:</p>
  <pre><code>public static void main(String[] args) throws IOException, InterruptedException {
    String year = args[0];
    String day = args[1];
    String session = System.getenv(&quot;AOCSESSION&quot;);
    System.out.println(session);
    
    HttpRequest req = HttpRequest.newBuilder()
        .uri(URI.create(&quot;https://adventofcode.com/&quot;+year+&quot;/day/&quot;+day+&quot;/input&quot;)).GET().build();
    CookieManager cookieManager = new CookieManager();
    HttpCookie cookie = new HttpCookie(&quot;session&quot;, session);
    cookieManager.getCookieStore().add(URI.create(&quot;https://adventofcode.com&quot;), cookie);
    HttpClient client = HttpClient.newBuilder().cookieHandler(cookieManager).build();
  
    String body = client.send(req, BodyHandlers.ofString()).body();
    System.out.println(body);
  }
  </code></pre>
  <p>However, the requests turn out unauthenticated (Response: <code>Puzzle inputs differ by user. Please log in to get your puzzle input.</code>). I already tried adding other fields to the cookie:</p>
  <pre><code>cookie.setDomain(&quot;.adventofcode.com&quot;);
  cookie.setMaxAge(Instant.parse(&quot;2031-11-01T18:26:23.293Z&quot;).getEpochSecond());
  cookie.setPath(&quot;/&quot;);
  cookie.setHttpOnly(true);
  cookie.setSecure(true);
  </code></pre>
  <p>but it doesn't solve the problem.</p>
  <p>How can I add a session cookie to the request?</p>