Apache-httpclient-form-based-login
Apache HttpClient-フォームベースのログイン
HttpClientライブラリを使用すると、リクエストを送信したり、パラメータを渡してフォームにログインしたりできます。
以下の手順に従って、フォームにログインします。
ステップ1-HttpClientオブジェクトを作成する
*HttpClients* クラスの* createDefault()*メソッドは、クラス *CloseableHttpClient* のオブジェクトを返します。これは、HttpClientインターフェイスの基本実装です。 このメソッドを使用して、HttpClientオブジェクトを作成します-
CloseableHttpClient httpClient = HttpClients.createDefault();
ステップ2-RequestBuilderオブジェクトを作成する
クラス RequestBuilder は、パラメーターを追加して要求を作成するために使用されます。 リクエストタイプがPUTまたはPOSTの場合、リクエストにパラメーターをURLエンコードエンティティとして追加します
post()メソッドを使用して、(POSTタイプの)RequestBuilderオブジェクトを作成します。
//Building the post request object
RequestBuilder reqbuilder = RequestBuilder.post();
ステップ3-UriとパラメーターをRequestBuilderに設定します。
RequestBuilderクラスの* setUri()および addParameter()*メソッドを使用して、URIおよびパラメーターをRequestBuilderオブジェクトに設定します。
//Set URI and parameters
RequestBuilder reqbuilder = reqbuilder.setUri("http://httpbin.org/post");
reqbuilder = reqbuilder1.addParameter("Name", "username").addParameter("password", "password");
ステップ4-HttpUriRequestオブジェクトを構築する
必須パラメーターを設定したら、* build()メソッドを使用して *HttpUriRequest オブジェクトをビルドします。
//Building the HttpUriRequest object
HttpUriRequest httppost = reqbuilder2.build();
ステップ5-リクエストを実行する
CloseableHttpClientオブジェクトのexecuteメソッドは、HttpUriRequest(インターフェイス)オブジェクト(つまり、 HttpGet、HttpPost、HttpPut、HttpHeadなど)と応答オブジェクトを返します。
前の手順で作成したHttpUriRequestを* execute()*メソッドに渡して実行します。
//Execute the request
HttpResponse httpresponse = httpclient.execute(httppost);
例
次の例は、ログイン資格情報を送信してフォームにログオンする方法を示しています。 ここでは、2つのパラメーター-*ユーザー名とパスワード*をフォームに送信し、メッセージエンティティとリクエストのステータスを出力しようとしました。
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
public class FormLoginExample {
public static void main(String args[]) throws Exception {
//Creating CloseableHttpClient object
CloseableHttpClient httpclient = HttpClients.createDefault();
//Creating the RequestBuilder object
RequestBuilder reqbuilder = RequestBuilder.post();
//Setting URI and parameters
RequestBuilder reqbuilder1 = reqbuilder.setUri("http://httpbin.org/post");
RequestBuilder reqbuilder2 = reqbuilder1.addParameter("Name",
"username").addParameter("password", "password");
//Building the HttpUriRequest object
HttpUriRequest httppost = reqbuilder2.build();
//Executing the request
HttpResponse httpresponse = httpclient.execute(httppost);
//Printing the status and the contents of the response
System.out.println(EntityUtils.toString(httpresponse.getEntity()));
System.out.println(httpresponse.getStatusLine());
}
}
出力
実行すると、上記のプログラムは次の出力を生成します-
{
"args": {},
"data": "",
"files": {},
"form": {
"Name": "username",
"password": "password"
},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "31",
"Content-Type": "application/x-www-form-urlencoded; charset = UTF-8",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_91)"
},
"json": null,
"origin": "117.216.245.180",
"url": "http://httpbin.org/post"
}
HTTP/1.1 200 OK
Cookieを使用したフォームログイン
フォームがデフォルトの CloseableHttpClient オブジェクトを作成する代わりに、Cookieを保存する場合。
- BasicCookieStoreクラスをインスタンス化して、CookieStoreオブジェクトを作成します。
//Creating a BasicCookieStore object
BasicCookieStore cookieStore = new BasicCookieStore();
- HttpClients クラスの* custom()*メソッドを使用してHttpClientBuilder *を作成します。
//Creating an HttpClientBuilder object
HttpClientBuilder clientbuilder = HttpClients.custom();
- setDefaultCookieStore()メソッドを使用して、Cookieストアをクライアントビルダーに設定します。
//Setting default cookie store to the client builder object
Clientbuilder = clientbuilder.setDefaultCookieStore(cookieStore);
- build()メソッドを使用して *CloseableHttpClient オブジェクトをビルドします。
//Building the CloseableHttpClient object
CloseableHttpClient httpclient = clientbuilder1.build();
リクエストの実行を渡すことにより、上記で指定した HttpUriRequest オブジェクトを作成します。
ページにCookieが保存されている場合、渡したパラメーターはCookieストアに追加されます。
パラメータを見ることができる CookieStore オブジェクトの内容を印刷できます(以前のページとともに、ケースに保存されたページ)。
Cookieを印刷するには、* getCookies()メソッドを使用して *CookieStore オブジェクトからすべてのCookieを取得します。 このメソッドは List オブジェクトを返します。 イテレータを使用して、以下に示すようにリストオブジェクトの内容を印刷します-
//Printing the cookies
List list = cookieStore.getCookies();
System.out.println("list of cookies");
Iterator it = list.iterator();
if(it.hasNext()) {
System.out.println(it.next());
}