3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Java] jsoupを使って、認証が必要なプロキシ経由でHTTPSアクセスする

Last updated at Posted at 2019-10-16

概要

jsoupを使ってプロキシ経由でアクセスする必要があったのですが、思いのほか手こずりました。

ポイントは以下の二点です。

  • 認証が必要なプロキシ経由でHTTPSアクセスをできないように、デフォルトで設定されている(Java 8から)。
  • Authenticator.setDefault()で、プロキシサーバーのユーザーIDとパスワードを指定する必要がある。

環境

jsoup

pom.xml
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.12.1</version>
</dependency>

Javaバージョン: 8

OK例

// 「認証が必要なプロキシ経由でのHTTPSアクセスを禁止」という設定を解除します。
// デフォルトではこのプロパティに"Basic"が指定されており、Basic認証が利用できないようになっていますので、"Basic"の設定を解除するため、空文字で上書きします。
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

// プロキシサーバーのID/パスワードをAuthenticator経由で渡すようにします。
Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("{プロキシサーバーのユーザーID}", "{プロキシサーバーのパスワード}".toCharArray());
    }
});

Jsoup.connect("{アクセスしたいURL}")
  .proxy("{プロキシサーバーのID or ホスト名}", {プロキシサーバーのポート番号})
  .get();

NG例

// Authorizationヘッダーを使うバージョン その1
Jsoup.connect("{アクセスしたいURL}")
  .header("Authorization", "Basic " + Base64.getEncoder().encodeToString("{プロキシサーバーのユーザーID}:{プロキシサーバーのパスワード}".getBytes()))
  .proxy("{プロキシサーバーのIPアドレス or ホスト名}", {プロキシサーバーのポート番号})
  .get();
// Authorizationヘッダーを使うバージョン その2 (ヘッダーの値に"Basic "の文字列を指定しない)
Jsoup.connect("{アクセスしたいURL}")
  .header("Authorization", Base64.getEncoder().encodeToString("{プロキシサーバーのユーザーID}:{プロキシサーバーのパスワード}".getBytes()))
  .proxy("{プロキシサーバーのIPアドレス or ホスト名}", {プロキシサーバーのポート番号})
  .get();
// Proxy-Authorizationヘッダーを使うバージョン その1
Jsoup.connect("{アクセスしたいURL}")
  .header("Proxy-Authorization", "Basic " + Base64.getEncoder().encodeToString("{プロキシサーバーのユーザーID}:{プロキシサーバーのパスワード}".getBytes()))
  .proxy("{プロキシサーバーのIPアドレス or ホスト名}", {プロキシサーバーのポート番号})
  .get();
// Proxy-Authorizationヘッダーを使うバージョン その2 (ヘッダーの値に"Basic "の文字列を指定しない)
Jsoup.connect("{アクセスしたいURL}")
  .header("Proxy-Authorization", Base64.getEncoder().encodeToString("{プロキシサーバーのユーザーID}:{プロキシサーバーのパスワード}".getBytes()))
  .proxy("{プロキシサーバーのIPアドレス or ホスト名}", {プロキシサーバーのポート番号})
  .get();
// 「jdk.http.auth.tunneling.disabledSchemes」の指定なし

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("{プロキシサーバーのユーザーID}", "{プロキシサーバーのパスワード}".toCharArray());
    }
});

Jsoup.connect("{アクセスしたいURL}")
  .proxy("{プロキシサーバーのID or ホスト名}", {プロキシサーバーのポート番号})
  .get();

いずれの場合も、以下のとおりエラーとなりました。

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"

参考(謝辞)

大変参考になりました!ありがとうございます!
https://qiita.com/kaakaa_hoe/items/d4fb11a3af035a287972

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?