gpt4 book ai didi

Java 登录 ASP.NET Web 窗体的方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:39:28 25 4
gpt4 key购买 nike

我正在开发一个 java 程序,它需要登录到 ASP.NET 网络表单,然后在通过身份验证后下载文件。正常的 HTTP GET/POST 不是问题,但是当我从 java 连接时,ASP 似乎没有给我 SESSION ID,而是来自浏览器。

当我在 Firefox 中查看 header 信息时,我看到从初始登录开始就设置了 cookie,但随后页面立即被重定向到一个新的 URL。我不确定它是否重要,但它在登录后重定向到的页面包含 iframe。我试过在里面加载主页和 iframe src,但都没有在标题中给我 cookie。

//Pull up the login page, extract out the hidden input variables __VIEWSTATE, __EVENTVALIDATION
URL url = new URL(loginPage);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
//This reads the page line-by-line and extracts out all the values from hidden input fields
Map<String,String> formFields = getViewstate(conn);

//Now re-open the URL to actually submit the POST data
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String postValues = URLEncoder.encode("txtUsername", "UTF-8") + "=" + URLEncoder.encode(uid, "UTF-8");
postValues += "&" + URLEncoder.encode("txtPassword", "UTF-8") + "=" + URLEncoder.encode(pwd, "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTTARGET", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
postValues += "&" + URLEncoder.encode("__VIEWSTATE", "UTF-8") + "=" + URLEncoder.encode(formFields.get("viewstate"), "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTVALIDATION", "UTF-8") + "=" + URLEncoder.encode(formFields.get("eventvalidation"), "UTF-8");
out.writeBytes(postValues);
out.flush();
out.close();
//At this point looking at Firefox sniffer data, it should be sending back the cookie
//However there is no Set-Cookie in the header fields
for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
// get ASP.NET_SessionId from cookie
if (key.equalsIgnoreCase("set-cookie")) {
sessionId = conn.getHeaderField(key);
sessionId = sessionId.substring(0, sessionId.indexOf(";"));
}
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
//The page it prints out is the page it was redirected to when logged in through the browser
System.out.println(line);
}
rd.close();
//At this point, it was a successful login, but I never got the cookie so I'm stuck

最佳答案

我认为 HtmlUnit 所基于的 HttpClient 具有我认为您正在寻找的较低级别的功能。很好地处理 cookie,但如果您需要更多,那么 Kurt 是对的,您应该寻找功能更多的东西。如果你真的需要获得完整的浏览器功能,你可以尝试像 Selenium/Webdriver 这样的东西,它实际上在程序控制下自动化浏览器。

关于Java 登录 ASP.NET Web 窗体的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4462742/

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