Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSOUP not sending Proxy-Authorization header #1155

Closed
Serhii-iLchenko opened this issue Dec 10, 2018 · 8 comments
Closed

JSOUP not sending Proxy-Authorization header #1155

Serhii-iLchenko opened this issue Dec 10, 2018 · 8 comments

Comments

@Serhii-iLchenko
Copy link

Hello, I've been trying to parse page through proxy, but cannot connect to proxy server.
Receiveing error: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"

Here is my code:

Connection connection = Jsoup.connect("https://google.com/");
connection.proxy("x.x.x.x", 1111);
String proxyCredentials = new String(Base64.encodeBase64("username:password".getBytes()));
connection.header("Proxy-Authorization", "Basic " + proxyCredentials);
connection.method(Method.GET);
connection.ignoreContentType(true);
try {
	Response response = connection.execute();
	Document document = response.parse();
	String text = document.body().wholeText();
	System.out.println(text);
} catch (IOException e) {
	e.printStackTrace();
}

And if I use java.net.Authenticator, the connection works OK.

Authenticator.setDefault(new Authenticator() {
	@Override
	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("username", "password".toCharArray());
	}
});
Connection connection = Jsoup.connect("https://google.com/");
connection.proxy("x.x.x.x", 1111);
connection.method(Method.GET);
connection.ignoreContentType(true);
try {
	Response response = connection.execute();
	Document document = response.parse();
	String text = document.body().wholeText();
	System.out.println(text);
} catch (IOException e) {
	e.printStackTrace();
}

I've read numerous SO threads, the only solution that I've found is using Authenticator.
What am I doing wrong? Please help.

I heve axamined the process with wireshark, and there were no "Proxy-Authorization" header sent at all.
I've also tried setting "Proxy-Authorization" as data and cookies, no result.

While I can use Authenticaor, I think that passing proxy username and password with header should be more convinient.

@jhy
Copy link
Owner

jhy commented Dec 23, 2018

If the Authenticator method works for you, what's the problem with using that?

From my understanding of how this works, the underlying Java Http handler has special case handling for proxy authentication, and if you're not using the Authenticator, it gets pulled out of the request, which is why you're not seeing it in wireshark.

@jhy jhy added the needs-more-info More information is needed from the reporter to progress the issue label Dec 23, 2018
@Serhii-iLchenko
Copy link
Author

Serhii-iLchenko commented Dec 26, 2018

Sending proxy username and proxy password through header is kinda connection-specific.
While setting global Authenticator is JVM-specific. If I use several connections with diffrent proxies that requre diffrent usernames and passwords, global authenticator will be much more error-prone.

@jhy
Copy link
Owner

jhy commented Dec 27, 2018

Got it. In your getPasswordAuthentication method, you can return null if you don't want to provide a password. The method is called on every hit, so you can check the host or URL to decide if you want to provide a password. See https://docs.oracle.com/javase/7/docs/api/java/net/Authenticator.html#getRequestingHost()

@jhy jhy closed this as completed Dec 27, 2018
@Serhii-iLchenko
Copy link
Author

Yeah, this is what I'm done so far. Thank you anyway.

@rublin
Copy link

rublin commented Sep 27, 2020

Hi!
For me, even Authenticator doesn't work. I really don't understand how to use the proxy with basic authentication here.

@benignoms
Copy link

This only occurs when the connection is with HTTPS. When you use multithreading there is no way to use the Default Authenticator if you need different users. Please help us. How to be able to link the credentials for each connection using JSOUP?

@varra4u
Copy link

varra4u commented Aug 9, 2022

I think still it doesn't have a way to provide credentials apart from using Authenticator (https://docs.oracle.com/javase/7/docs/api/java/net/Authenticator.html#getRequestingHost()), can someone confirm pls?

@jhy jhy removed the needs-more-info More information is needed from the reporter to progress the issue label Nov 9, 2023
@jhy
Copy link
Owner

jhy commented Nov 9, 2023

I have added direct support for per-request authentication in Jsoup.connect().auth() in #2046. Would be greatly appreciated if folks can test this in a local snapshot version and provide feedback (on the PR - #2046)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants