在我的搜寻器中接受条款和条件后,我需要访问一个页面。但是,即使在源代码中使用了url之后:
'/auth/submitterms.do?randomNum=' + randomNum
返回的响应正文是相同的条款和条件页面。 当我在Postman中执行相同操作时,它可以正常工作,并将我带到下一个
randomNum
是使用正则表达式从响应正文中获得的。我使用cookieHandler API来处理会话。
代码段: GET请求到条款和条件页面:
connection = new URL(URL_NEXIS+"auth/ipmdelegate.do").openConnection();
res = connection.getInputStream();
try (Scanner scanner = new Scanner(res)){
response = scanner.useDelimiter("\\A").next();
System.out.println("Terms Page: \n"+response);
}
Regex用于从响应中获取randomNum:
Pattern pattern = Pattern.compile("randomNum=[0-9].[0-9]*");
Matcher matcher = pattern.matcher(response);
if (matcher.find()) {
System.out.println(matcher.group());
randomNum = matcher.group().split("=")[1];
System.out.println(randomNum.toString());
} else {
throw new IOException("Error: Could not accept terms and conditions.");
}
获取对“ 接受”条款的网址的请求。
System.out.println(URL_NEXIS + "auth/submitterms.do?randomNum=" + randomNum);
redirect_page = URL_NEXIS + "auth/submitterms.do?randomNum=" + randomNum;
connection = new URL(redirect_page).openConnection();
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
res = connection.getInputStream();
这如何实现?为什么同一个请求与Postman的行为不同?
编辑: 我使用了connection.setInstanceFollowRedirect(false);并能够获得302响应码。现在,我可以看到位置标头和set-cookie变量。但是,我认为cookieHandler api已经可以处理该会话。
现在,当我尝试从位置标头向新网址发送GET请求时,回到条款和条件页面。