gpt4 book ai didi

java - 使用 App Engine url 获取发布用户凭据时遇到问题

转载 作者:行者123 更新时间:2023-12-01 04:56:36 28 4
gpt4 key购买 nike

我正在开发一个应用程序,它需要能够代表用户登录网站并进行一些 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,或者继续使用 python,如果这会让它变得更容易的话。这是一个大项目的一小部分,我不太愿意切换。

最佳答案

经过几天的努力,我发现了这个 article这正是我在 python 中尝试做的事情。我决定在这个项目中使用 python,并且我将使用 BeautifulSoup 进行 html 抓取。仍然不确定我的代码最初出了什么问题。

关于java - 使用 App Engine url 获取发布用户凭据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016028/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com