小编Sim*_*ars的帖子

使用 Java 11 HttpClient 设置会话 cookie

我正在尝试使用 Java 11 HttpClient 获取页面的内容。由于该页面使用 OAuth,我想使用我的会话 cookie 进行身份验证。受这个问题的启发,我目前使用以下代码:

public static void main(String[] args) throws IOException, InterruptedException {
  String year = args[0];
  String day = args[1];
  String session = System.getenv("AOCSESSION");
  System.out.println(session);
  
  HttpRequest req = HttpRequest.newBuilder()
      .uri(URI.create("https://adventofcode.com/"+year+"/day/"+day+"/input")).GET().build();
  CookieManager cookieManager = new CookieManager();
  HttpCookie cookie = new HttpCookie("session", session);
  cookieManager.getCookieStore().add(URI.create("https://adventofcode.com"), cookie);
  HttpClient client = HttpClient.newBuilder().cookieHandler(cookieManager).build();

  String body = client.send(req, BodyHandlers.ofString()).body();
  System.out.println(body);
}
Run Code Online (Sandbox Code Playgroud)

然而,请求结果未经身份验证(响应:)Puzzle inputs differ by user. Please log in to get your puzzle input.。我已经尝试向 cookie 添加其他字段:

cookie.setDomain(".adventofcode.com");
cookie.setMaxAge(Instant.parse("2031-11-01T18:26:23.293Z").getEpochSecond()); …
Run Code Online (Sandbox Code Playgroud)

java cookies httpclient java-11

9
推荐指数
2
解决办法
8405
查看次数

Java - 从中​​间开始迭代二维数组

我在二维数组上有这个完全正常的循环.

for(int i = 0; i<array.length; i++){
    for(int j = 0; i<array.length; j++){
        array[i][j].doSomething();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想通过这个二维数组,从中间开始.例如,如果两个维度的数组长度都为100,我希望它像这样重复:

array[50][50] //middle of array
array[49][50] //x-1
array[50][49] //y-1
array[51][50] //x+1
array[50][51] //y+1
array[48][50] //x-2
array[49][49] //x-1 and y-1
array[50][48] //y-2
array[51][49] //x+1 and y-1
array[52][50] //x+2
array[51][51] //x+1 and y+1
array[50][52] //y+2
array[49][51] //x-1 and y+1
etc.
Run Code Online (Sandbox Code Playgroud)

我花了好几个小时找到一种有效的方法并在互联网上找到解决方案,但我还没有找到一个好的答案.谁知道怎么做?

java arrays

3
推荐指数
1
解决办法
256
查看次数

时间戳是否等效于LAST_INSERT_ID?

我喜欢MySQL的LAST_INSERT_ID()函数.我一直用它来检索刚插入的行的id,然后从我的存储过程返回.但是,现在我有一个表,其TIMESTAMP作为主键设置为DEFAULT CURRENT_TIMESTAMP.如何检索最后插入的时间戳?

mysql timestamp last-insert-id

1
推荐指数
1
解决办法
225
查看次数

标签 统计

java ×2

arrays ×1

cookies ×1

httpclient ×1

java-11 ×1

last-insert-id ×1

mysql ×1

timestamp ×1