は私がサンプルコード(ここではSOの私の前の回答の1からのサンプルコード)を使用してお見せしましょう:
public CookieStore sendPostData(String url, String user, String pass) {
// Setup a HTTP client, HttpPost (that contains data you wanna send) and
// a HttpResponse that gonna catch a response.
DefaultHttpClient postClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
try {
// Make a List. Increase the size as you wish.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Add your form name and a text that belongs to the actual form.
nameValuePairs.add(new BasicNameValuePair("username_form", user));
nameValuePairs.add(new BasicNameValuePair("password_form", pass));
// Set the entity of your HttpPost.
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute your request against the given url and catch the response.
response = postClient.execute(httpPost);
// Status code 200 == successfully posted data.
if(response.getStatusLine().getStatusCode() == 200) {
// Green light. Catch your cookies from your HTTP response.
CookieStore cookies = postClient.getCookieStore();
return cookies;
}
} catch (Exception e) {
}
}
は、今すぐあなたのクッキーを設定する(またはチェック/それらを検証)する必要があり、あなたに対して要求を行う前に、サーバ。
サンプルコード:
CookieStore cookieStore = sendPostData("www.mypage.com/login", "Username",
"Password");
// Note, you may get more than one cookie, therefore this list.
List<Cookie> cookie = cookieStore.getCookies();
// Grab the name of your cookie.
String cookieOne = cookie.get(0).getName();
あなたが本当に行う必要が何なWiresharkとして有益なツールを使用して、あなたのHTTP response
をチェックすることです。コンピュータのブラウザからログインし、あなたの応答(Java/Androidコードでは、値を取得するためにString value = cookie.get(0).getValue();
を使用しています)の正しい値を確認してください。
これは、あなたのドメインのクッキーを設定する方法である:
// Grab the domain of your cookie.
String cookieOneDomain = cookie.get(0).getDomain();
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(cookieOneDomain, cookieOne);
はただ、他の2つの答えに追加する:あなたはWebViewのを使用する場合、プレーン*ウェブ*アプリになるだろう。それ以外の場合は、AsyncTaskを使用してUIスレッドをブロックしないようにしてください。 – bigstones