achievoインスタンスに対してログインを実行するJavaプログラムを作成しようとしています。 Screen Scrapingを使用しようとしています。ブラウザはサポートフレームをサポートしません
<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>
を明らかに私が手:首尾よくログインする-で、私はそれが言うのメインページにリダイレクトアチーボとき、しかし
@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を実行するために必要です。
私はおそらくディスパッチ.phpフレームにアクセスしようとしましたが、これは私のセッションが期限切れであり、再ログインする必要があると報告しています。
フレームが偽装されることはありますか?または、何とか接続を維持し、URLを変更し、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を使用してdispatch.phpフレームを取得するにはどうすればいいですか?
displatch.phpにアクセスしようとすると、セッション内のCookieと[referrer header](http://en.wikipedia.org/wiki/HTTP_referrer)を渡す必要があります。 Cookie管理を自動的に行うので、Apache HTTPClientを使用することを強くお勧めします。これにより、httpリソースに簡単にアクセスできるようになります。 – Augusto