あなたが本当にやりたいことがわかったかどうかはわかりませんが、少なくとも、認証プロセスを実行してデータ/情報を取得するには、いくつかの Get/Post 操作が必要になると確信しています。次の方法を確認してください。
public StringBuilder post(String url, List<NameValuePair> nvps) {
try {
HttpPost httppost = new HttpPost(url);
// if there is cookies, set then
if (cookies != null && cookies.size() > 0) {
String cookieString = "";
for (int i = 0; i < cookies.size(); ++i) {
cookieString += cookies.get(i).getName() + "=" + cookies.get(i).getValue() + "; ";
}
cookieString += "domain=" + Constants.BaseUrl + "; " + "path=/";
httppost.addHeader("Cookie", cookieString);
}
// connection timeout options
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, Constants.timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, Constants.timeoutSocket);
// setup the post method
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;)
builder.append(line).append("\n");
// set cookies
List<Cookie> incomingCookies = httpClient.getCookieStore().getCookies();
for (int i = 0; incomingCookies != null && i < incomingCookies.size(); i++) {
cookies.add(incomingCookies.get(i));
}
return builder;
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (Exception e) {
}
return null;
}
上記のメソッドは、ヘッダーの引数としてペア値nvpsを使用して、 url文字列パラメーターでポストを実行します。Constantsクラスは WebService の静的文字列 (API エントリなど) を宣言するクラスであり、フィールドcookiesはセッションの問題を保持する Cookie のリストであることに注意してください。このメソッドは、POST リクエストの結果を文字列ビルダー オブジェクトとして返します。基本的に、これはいくつかのケースで使用できる汎用的な方法であり、タスクに合わせて少し調整する必要があります。これは、認証に使用できるものです。
別の重要なメソッドである HTTP GET があります。
public StringBuilder get(String url) {
try {
HttpGet httpget = new HttpGet(url);
// if there is cookies, set then
if (cookies != null && cookies.size() > 0) {
String cookieString = "";
for (int i = 0; i < cookies.size(); ++i) {
cookieString += cookies.get(i).getName() + "=" + cookies.get(i).getValue() + "; ";
}
cookieString += "domain=" + Constants.BaseUrl + "; " + "path=/";
httpget.addHeader("Cookie", cookieString);
}
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, Constants.timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, Constants.timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpget);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;)
builder.append(line).append("\n");
// set cookies
List<Cookie> incomingCookies = httpClient.getCookieStore().getCookies();
for (int i = 0; incomingCookies != null && i < incomingCookies.size(); i++) {
cookies.add(incomingCookies.get(i));
}
return builder;
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (Exception e) {
}
return null;
}
これは同様の一連の手順を実行しますが、目的は大きく異なります。これの目的は、すでに認証済みであるか、認証プロセスが不要であることを考慮して、情報を取得することです。要求されたデータを文字列ビルダーとして返します。
これらの方法は非常に一般的であるだけでなく、要求された Web ページで使用されているプロセスを注意深く確認する必要があることに注意してください。何らかの形でお役に立てば幸いです!;-)