[Java] Use jsoup to access HTTPS via a proxy that requires authentication

Overview

I had to access it via a proxy using jsoup, but it was unexpectedly difficult.

The points are the following two points.

environment

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 version: 8

OK example

//Cancel the setting "Prohibit HTTPS access via proxy that requires authentication".
//By default this property"Basic"Is specified and Basic authentication is not available, so"Basic"Overwrite with an empty string to cancel the setting of.
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

//Proxy server ID/Make sure to pass the password via Authenticator.
Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("{Proxy server user ID}", "{Proxy server password}".toCharArray());
    }
});

Jsoup.connect("{URL you want to access}")
  .proxy("{Proxy server ID or host name}", {Proxy server port number})
  .get();

NG example

//Version 1 that uses the Authorization header
Jsoup.connect("{URL you want to access}")
  .header("Authorization", "Basic " + Base64.getEncoder().encodeToString("{Proxy server user ID}:{Proxy server password}".getBytes()))
  .proxy("{Proxy server IP address or host name}", {Proxy server port number})
  .get();
//Version that uses the Authorization header Part 2 (in the header value"Basic "Do not specify the character string of)
Jsoup.connect("{URL you want to access}")
  .header("Authorization", Base64.getEncoder().encodeToString("{Proxy server user ID}:{Proxy server password}".getBytes()))
  .proxy("{Proxy server IP address or host name}", {Proxy server port number})
  .get();
// Proxy-Version 1 that uses the Authorization header
Jsoup.connect("{URL you want to access}")
  .header("Proxy-Authorization", "Basic " + Base64.getEncoder().encodeToString("{Proxy server user ID}:{Proxy server password}".getBytes()))
  .proxy("{Proxy server IP address or host name}", {Proxy server port number})
  .get();
// Proxy-Version that uses the Authorization header Part 2 (in the header value"Basic "Do not specify the character string of)
Jsoup.connect("{URL you want to access}")
  .header("Proxy-Authorization", Base64.getEncoder().encodeToString("{Proxy server user ID}:{Proxy server password}".getBytes()))
  .proxy("{Proxy server IP address or host name}", {Proxy server port number})
  .get();
// 「jdk.http.auth.tunneling."disabled Schemes" is not specified

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("{Proxy server user ID}", "{Proxy server password}".toCharArray());
    }
});

Jsoup.connect("{URL you want to access}")
  .proxy("{Proxy server ID or host name}", {Proxy server port number})
  .get();

In either case, the following error occurred.

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

Reference (Acknowledgment)

It was very helpful! Thank you! https://qiita.com/kaakaa_hoe/items/d4fb11a3af035a287972

Recommended Posts

[Java] Use jsoup to access HTTPS via a proxy that requires authentication
407 error when trying to access an HTTPS site in Java via an authenticated proxy
How to quickly create a reverse proxy that supports HTTPS with Docker
[Java] How to use substring to cut out a character string
How to set up a proxy with authentication in Feign
Study Java: Use Timer to create something like a bomb timer
A story about misunderstanding how to use java scanner (memo)
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
[java] Reasons to use static
How to use Java variables
[Java] How to use Optional ①
A story that I struggled to challenge a competition professional with Java
I want to use swipeback on a screen that uses XLPagerTabStrip
How to implement a job that uses Java API in JobScheduler
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~