我正在开发一个应用程序,它需要能够代表用户登录网站并进行一些html抓取。像许多其他开发人员一样,app引擎在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或者继续使用它,如果这样会让它变得更容易。这是一个很大的项目中的一小部分,我不会太过切换。
答案 0 :(得分:0)
经过几天的挣扎,我发现了article 这正是我在python中尝试做的事情。我决定在这个项目中使用python,我将使用BeautifulSoup进行html抓取。仍然不确定我的代码最初有什么问题。