[~/Desktop]$ keytool -keystore ./keystore -import -file hondou.homedns.org Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8 キーストアのパスワードを入力してください: password 新規パスワードを再入力してください: password ...とりあえず keystore のパスワードは password にした。
ひとまず HTTPS で、GET、POST、ステータスラインの取得、Cookie の利用まで試した。後日のコピペ元としてはこれでいいかな
/** * Http Client のサンプルプログラム. */ public final class App { /** * Keystore のパスワード. */ private static final char[] KEY_STORE_PASS = "password".toCharArray(); /** * Proxy サーバの IP アドレス. */ private static final String PROXY_HOST = "192.168.1.2"; /** * Proxy サーバの Port 番号. */ private static final int PROXY_PORT = 3128; /** * デフォルトコンストラクタの無効化. */ private App() { super(); } /** * main. * @param args コマンド引数 */ public static void main(final String[] args) { // // 現在有効な暗号化アルゴリズムの列挙 // for (Provider provider : Security.getProviders()) { // System.out.println("\"" + provider.getName() + "\""); // System.out.println(provider.getInfo()); // System.out.println(); // } try { final DefaultHttpClient httpClient = new DefaultHttpClient(); // ---- SSL (HTTPS) の設定 // KeyStore.getDefaultType() : SUNPKCS11, SUNPKCS11-dawin, etc. final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); final InputStream keyIs = App.class.getClassLoader().getResourceAsStream("keystore"); keyStore.load(keyIs, KEY_STORE_PASS); keyIs.close(); final TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm()); tmf.init(keyStore); final SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, tmf.getTrustManagers(), null); // 証明書のURLを検証しない final SSLSocketFactory socektFacotry = new SSLSocketFactory( ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); final Scheme scheme = new Scheme("https", 443, socektFacotry); httpClient.getConnectionManager().getSchemeRegistry().register(scheme); // ---- Proxy の設定 final HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); // ---- HTTP GET (ログイン画面) final HttpGet loginGet = new HttpGet("https://hondou.homedns.org/squirrelmail/src/login.php"); final HttpResponse loginRes = httpClient.execute(loginGet); System.out.println(EntityUtils.toString(loginRes.getEntity())); EntityUtils.consume(loginRes.getEntity()); // ---- Cookie の取得 // (httpClient を使い回せば自動的に次回の Request に添付されるので、通常は操作する // 必要なし) // for (Cookie cookie : httpClient.getCookieStore().getCookies()) { // System.out.println(cookie.getName() + ":" + cookie.getValue()); // } // ---- ログイン処理 // <form action="redirect.php" method="post" name="login_form" > // <input type="text" name="login_username"/> // <input type="password" name="secretkey"/> // <input type="submit" value="ログイン" /> // </form> final List<NameValuePair> reqParamList = new ArrayList<NameValuePair>(); reqParamList.add(new BasicNameValuePair("login_username", "*********")); reqParamList.add(new BasicNameValuePair("secretkey", "**********")); UrlEncodedFormEntity reqForm = new UrlEncodedFormEntity(reqParamList); final HttpPost redirectPost = new HttpPost("https://hondou.homedns.org/squirrelmail/src/redirect.php"); redirectPost.setEntity(reqForm); final HttpResponse redirectRes = httpClient.execute(redirectPost); System.out.println("ログイン結果"); System.out.println(redirectRes.getStatusLine()); for (Header header : redirectRes.getAllHeaders()) { System.out.println(header); } } catch (Exception ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); } } }
KeyStore.getInstance("SUNPKCS11")とするんではなく、素直に
KeyStore.getInstance(KeyStore.getDefaultType())としておけば早く寝れたのに・・・
String value = StringUtils.substringBetween(res,"name=\"price\" value=\"", "\">");
String[] options = StringUtils.substringsBetween(res,"SELECTED>", "</OPTION");