次のことを実行するAndroidアプリを作成しています。
- https(SSL!)ページのフォームを使用してログインし、Cookieを受信します
- httpGETアクションを発行してhtmlを取得します
- そのhtmlを解析し、ビューやリストなどに表示します。
私はかなり前からJsoup、httpUnit、HTMLUnitをいじっていましたが、いくつかの問題が発生しています。
A.ログインは問題なく機能します。(Webサイトのウェルカムページが表示されます)が、GETステートメントを発行すると(Cookieを含めると)、ログインフォームにリダイレクトされます。したがって、応答htmlは私が期待したものではありません。(keepalivestrategyと関係があるかもしれませんか?)
B. InputBuffersは小さすぎて、HTMLページ全体を受け取り、解析用に設定できません。
注意:私はウェブサーバーを制御できません
私はこれがまったく新しいので、チュートリアルまたはコードスニペットが役立ちます。
たとえば、これは私がWebサイトにログインするために使用するものです。
public int checkLogin() throws Exception {
ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("userid", getUsername()));
data.add(new BasicNameValuePair("password", getPassword()));
data.add(new BasicNameValuePair("submit_login", "Logmein"));
Log.d(TAG, "Cookie name : " + getCookieName());
Log.d(TAG, "Cookie cont : " + getCookie());
HttpPost request = new HttpPost(BASE_URL);
request.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
request.getParams().setParameter("http.protocol.handle-redirects",false);
request.setEntity(new UrlEncodedFormEntity(data, "UTF-8"));
HttpResponse response;
httpsclient.getCookieStore().clear();
List<Cookie> cookies = httpsclient.getCookieStore().getCookies();
Log.d(TAG, "Number of Cookies pre-login : " + cookies.size());
response = httpsclient.execute(request);
cookies = httpsclient.getCookieStore().getCookies();
Log.d(TAG, "Number of Cookies post-login : " + cookies.size());
String html = "";
// Problem : buffer is too small!
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line);
}
in.close();
html = str.toString();
Document doc = Jsoup.parse(html);
Log.v(TAG, "Ik heb nu dit : " + doc.toString());
if (cookies.size() > 0){
storeCookie(cookies.get(0).getName(), cookies.get(0).getValue());
return MensaMobileActivity.REQUEST_SUCCESS;
} else {
return MensaMobileActivity.REQUEST_ERROR;
}
}