gpt4 book ai didi

java - 发布到 Siteminder login.fcc 以使用 java 检索 cookie

转载 作者:行者123 更新时间:2023-12-02 11:20:46 25 4
gpt4 key购买 nike

我的应用程序需要访问一些受 SiteMinder 保护的 URL,因此需要 SiteMinder 代理提供的 SMSession cookie。

我想通过将我的登录凭据发布到 Siteminder 登录表单来获取 SMSession cookie。

我尝试了许多不同的方法来访问所需数据(目标、smauthreason、smagentname、用户名、密码)并将其发布到 login.fcc。我可以像这样在 Postman/Fiddler 中发布它并检索 cookie。

try {
URL requestURL = new URL("HTTPS_URL_TO_THE/login-captcha.fcc");
HttpsURLConnection conn = (HttpsURLConnection) requestURL.openConnection();
conn.setDoOutput(true);
//conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "HOST_FROM_ABOVE");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Length","" + createBody().getBytes().length);
conn.setRequestProperty("Cache-Control", "max-age=0");
conn.setRequestProperty("Origin", "ORIGIN_HOST_FROM_WHERE_POSTED");
conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.setRequestProperty("DNT", "1");
conn.setRequestProperty("Referer", "REFERER_TAKEN_FROM_FIDDLER");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
conn.setRequestProperty("Accept-Language", "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7");


OutputStream os = conn.getOutputStream();
os.write(createBody().getBytes());

BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String in;
while ((in = br.readLine()) != null){
System.out.println(in);
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

当我之前提供cookie时,我将获得连接并可以访问该页面。但在这一步中我想检索 cookie。

body 数据100%正确。上例中使用的 header 并不是全部都需要。这是我最后一次绝望的尝试发布与 fiddler 中所示完全相同的内容。但我无法登录或 302 HTTP 重定向。

为什么不起作用?有什么建议吗?

最佳答案

我的问题得到了解决方案。事实上,我必须手动处理重定向。如果我自动遵循重定向,则检索到的 cookie 将不会随下一个请求一起发送。在没有 cookie 的情况下发布到 targetURL 会自动重定向到登录页面。

URL protectedURL = new URL(protectedURLString);
HttpsURLConnection conn = (HttpsURLConnection) protectedURL.openConnection();
conn.setInstanceFollowRedirects(false);

boolean redirect = false;
System.out.println("Request URL: " + protectedURL);

//Check for redirection
int status = conn.getResponseCode();
if(status != HttpURLConnection.HTTP_OK) {
if(status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
redirect = true;
}
}
System.out.println("Response Code: " + status);

//If redirected
if(redirect) {
//get new redirect URL from Location header field
String newURL = conn.getHeaderField("Location");
URL tempURL = new URL(newURL);
String query = tempURL.getQuery();
//Read parameters from query
String [] params = query.split("&");
for (String param : params) {
String [] parts = param.split("=");
String key = parts[0].toLowerCase();
String value = "";
if(parts.length > 1)
value = parts[1];
updateVars(key, value);
}

}
postToLogin(this.URL_LOGIN);

这是我的解决方案中使用的一些代码。 updateVars(key, value) 更新类变量。 postToLogin(this.URL_LOGIN) 发布到包含使用类变量生成的正文的登录 URL。

关于java - 发布到 Siteminder login.fcc 以使用 java 检索 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49935799/

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