我正在与一位朋友合作,以便能够登录我们的学校服务器并能够在不使用浏览器的情况下抓取一些网页。 我们已尝试使用一些建议的方法,但无法将重定向传递到登录页面。
我意识到以下几点;
每个会话都需要一个cookie,但是当我打印出连接" Set-Cookie"值,我得[] - >意思是没有cookie。
有没有人可以帮助我们成功发布我们的凭据,并能够从登录页面重定向到我们的目标URL?
这是我们的代码:
public static void Login() {
String charset = "ISO-8859-1";//
try {
String userid = "useridhere";
String password = "passwordhere";
String Submit = "Login";
String query = String.format("netid=%s&password=%s&Submit=%s",
URLEncoder.encode("netid", charset),
URLEncoder.encode("password", charset),
URLEncoder.encode("Submit", charset));
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
java.net.CookieStore cookieJar = manager.getCookieStore();
URL url = new URL("url"+"?"+query);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", charset);
List<String> c = connection.getHeaderFields().get("Set-Cookie");
System.out.println(c);
InputStream response = connection.getInputStream();
try (Scanner scanner = new Scanner(response)) {
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
connection.setInstanceFollowRedirects(true);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
response = connection.getInputStream();
try (Scanner scanner = new Scanner(response)) {
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
} catch(Exception e) {
System.out.println("error occured");
e.printStackTrace();
}
}