我正在尝试创建一个针对achievo实例执行登录的java程序。我正在尝试使用Screen Scraping。
我设法使用以下代码登录:
@Test
public void testLogin() throws Exception {
HashMap<String, String> data = new HashMap<String, String>();
data.put("auth_user", "user");
data.put("auth_pw", "password");
doSubmit("https://someurl.com/achievo/index.php", data);
}
private void doSubmit(String url, HashMap<String, String> data) throws Exception {
URL siteUrl = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) siteUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
//conn.setRequestProperty( "User-agent", "spider" );
//conn.setRequestProperty("User-agent", "Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.01");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
Set<String> keys = data.keySet();
Iterator<String> keyIter = keys.iterator();
StringBuilder content = new StringBuilder("");
for(int i=0; keyIter.hasNext(); i++) {
Object key = keyIter.next();
if(i!=0) {
content.append("&");
}
content.append(key + "=" + URLEncoder.encode(data.get(key), "UTF-8"));
}
System.out.println(content.toString());
out.writeBytes(content.toString());
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while((line=in.readLine())!=null) {
System.out.println(line);
}
in.close();
}
然而,当achievo成功登录后,我会被重定向到主页,其中显示:
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<title>Achievo</title>
</head>
<frameset rows="113,*" frameborder="0" border="0">
<frame name="top" scrolling="no" noresize src="top.php?atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43" marginwidth="0" marginheight="0">
<frameset cols="210,*" frameborder="0" border="0">
<frame name="menu" scrolling="no" noresize src="menu.php?atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43" marginwidth="0" marginheight="0">
<frame name="main" scrolling="auto" noresize src="dispatch.php?atknodetype=pim.pim&atkaction=pim&atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43" marginwidth="0" marginheight="0">
</frameset>
<noframes>
<body bgcolor="#CCCCCC" text="#000000">
<p>Your browser doesnt support frames, but this is required to run Achievo</p>
</body>
</noframes>
</frameset>
显然我得到了你的浏览器不支持框架,但这需要运行大展。
我试图直接访问dispatch.php框架,因为这是我可能想要的,但是,它报告我的会话已经过期,并且我需要重新登录。
有没有假装框架?或者以某种方式保持连接,更改网址,并尝试获取dispatch.php框架?
使用HtmlUnit,我做了以下事情:
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
HtmlPage page = webClient.getPage("https://someurl.com/index.php");
System.out.println(page.asXml());
List<HtmlForm> forms = page.getForms();
assertTrue(forms != null && !forms.isEmpty());
HtmlForm form = forms.get(0);
HtmlSubmitInput submit = form.getInputByName("login");
HtmlInput inputUsername = form.getInputByName("auth_user");
HtmlInput inputPw = form.getInputByName("auth_pw");
inputUsername.setValueAttribute("foo");
inputPw.setValueAttribute("bar");
HtmlPage page2 = submit.click();
CookieManager cookieManager = webClient.getCookieManager();
Set<Cookie> cookies = cookieManager.getCookies();
System.out.println("Is cookie " + cookieManager.isCookiesEnabled());
for(Cookie cookie : cookies) {
System.out.println(cookie.toString());
}
System.out.println(page2.asXml());
webClient.closeAllWindows();
我在这里获取表单,我提交它,然后检索相同的消息。当我也打印出来时,我可以看到我有一个cookie。现在的问题是,如何使用登录的cookie继续获取dispatch.php框架?
答案 0 :(得分:1)
这种刮痧有点复杂,需要考虑几个因素。
我建议使用比标准Java URL提供程序更全面的Apache HttpClient module,并为您管理Cookie等内容。
答案 1 :(得分:1)
您必须提取主框架的网址(dispatch.php?atknodetype=pim.pim&atkaction=pim&atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43
)并向此网址发出第二个请求。如果使用cookie来跟踪会话,您还必须将响应中包含的cookie发送到您的登录请求。
我会使用更高级别的API(例如Apache HttpClient),甚至像HtmlUnit这样的程序化浏览器。