我正在开发一个应用程序,该应用程序需要能够代表用户登录网站并进行一些 html 抓取。像许多其他开发人员一样,应用引擎在 cookie 管理方面给我带来了麻烦。我登录的服务器在初始 POST 之后发送重定向,然后将另一个重定向发送到最终登录页面。据我所知,目的是让服务器验证 cookie 是否正常工作。我已经从 SO 上的其他答案中拼接了以下帮助程序类。
public class Utilities {
public static String smartPost(String url, String data) throws IOException {
// storage for cookies between redirects
Map<String, String> cookies = new HashMap<String, String>();
HttpURLConnection connection;
StringBuilder response = new StringBuilder();
response.append(url);
URL resource = new URL(url);
connection = (HttpURLConnection) resource.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",
"" + Integer.toString(data.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
url = connection.getHeaderField("location");
while (url != null) {
// Get Cookies
getCookiesFromConnection(connection, cookies);
URL redirectResource = new URL(url);
response.append(url);
connection = (HttpURLConnection) redirectResource.openConnection();
connection.setRequestMethod("GET");
addCookiesToConnection(connection, cookies);
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setDoInput(true);
url = connection.getHeaderField("location");
connection.disconnect();
}
// Arrived at final location
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
}
static void addCookiesToConnection(HttpURLConnection c,
Map<String, String> storage) {
StringBuilder cookieStringBuilder = new StringBuilder();
for (Entry<String, String> e : storage.entrySet()) {
cookieStringBuilder.append(e.getKey());
cookieStringBuilder.append("=");
cookieStringBuilder.append(e.getValue());
cookieStringBuilder.append(";");
}
c.setRequestProperty("Cookies", cookieStringBuilder.toString());
}
static void getCookiesFromConnection(HttpURLConnection c,
Map<String, String> storage) {
Map<String, List<String>> headers = c.getHeaderFields();
for (Entry<String, List<String>> e : headers.entrySet()) {
if (e.getKey().equalsIgnoreCase("Set-Cookie")) {
for (String cookieHeader : e.getValue()) {
String cookie = cookieHeader.substring(0,
cookieHeader.indexOf(";"));
String key = cookie.substring(0, cookie.indexOf("="));
String value = cookie.substring(cookie.indexOf("=") + 1);
storage.put(key, value);
}
}
}
}
}
我的目标是手动处理重定向并将 cookie 传递到最终页面。它在开发服务器上运行良好,但我不认为这是我的代码在工作,而是本地服务器上的默认行为。有人有在生产服务器上实现这种功能的经验吗?我对 java.net 包非常缺乏经验,所以我可能离解决方案还很远。
我最初尝试在 Go 中实现这一点,但我得到了相同的结果,并认为这只是我完全缺乏 Go 的经验。由于 Jsoup,Java 无论如何都会更容易进行 html 抓取,但我不反对使用 python 或继续这样做,如果这会以某种方式使其更容易。这是一个大项目的一小部分,我不会太远切换。