commons httpclient3.1でBasic認証

httpclient3.1以上にて、ググって出てくるBasic認証突破術をそのまま書くとDeprecatedになってしまうようです。

HttpClient client = new HttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "password");
client.getState().setCredentials(null, null, credentials);
GetMethod method = new GetMethod(url);
method.setDoAuthentication(true);

これは3.0系まで。

3.1でDeprecatedされないためにはこう書く。

HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
client.getState().setCredentials(
		new AuthScope("domain", 80, "realm"),
		new UsernamePasswordCredentials("user", "pass"));
method.setDoAuthentication(true);
int statusCode = client.executeMethod(method);

BasicScheme (HttpClient 3.1 API)

RFC2617で策定された仕様にのっとったようだ。