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

HTTPS request throught Proxy not working (but working fine with java.net) #8233

Closed
chuckdu21 opened this issue Feb 2, 2024 · 10 comments
Closed

Comments

@chuckdu21
Copy link

Hi,

I need to request an API (HTTPS) throught a Proxy.
This code below with java.net is working fine.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("url proxy", 3128));
String url = new URL(null, "an https url", new sun.net.www.protocol.https.Handler());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(proxy);
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(getParamsString(parameters));
out.flush();
out.close();

InputStream stream;
if (con.getResponseCode() < 400) {
stream = con.getInputStream();
} else {
stream = con.getErrorStream();
}
...

I'm trying to renew this code with OkHttp API. I can request any HTTP url but not HTTPS, everytime i get the same Exception "Failed to authenticate with proxy".
Here's my code:

OkHttpClient client = new OkHttpClient.Builder()
		.proxy(p)
		//.hostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier())
		//.socketFactory(HttpsURLConnection.getDefaultSSLSocketFactory())
		//.socketFactory(LazySSLSocketFactory.getDefault())
		//.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustmanager[0])
		.build();

RequestBody body = new FormBody.Builder()
		.add("some", "param")
		.build();

Request request = new Request.Builder()
		.header("Accept", "application/json")
		.url("an https url")
		.post(body)
		.build();

Call call = client.newCall(request);
Response rep = call.execute();

As you can see, I tried to set socketFactory in different way.
I already check some other issues with the same problem but no solution...

Any idea why it's not working with okhttp but working with java.net ?

@yschimke
Copy link
Collaborator

yschimke commented Feb 3, 2024

I'm trying to reproduce on #8235

While I have a failure, I'm not sure it's the same, but I'll keep digging.

@yschimke
Copy link
Collaborator

yschimke commented Feb 3, 2024

Looking a bit more at your case, it's failing as unauthenticated. But I don't think URLConnection supports authentication with http proxies either. So it's strange.

If you have a username and password, you can set the proxyAuthenticator on the OkHttpClient.

It's used here, which is why you are seeing that failure message.

      when (response.code) {
        HttpURLConnection.HTTP_OK -> return null

        HttpURLConnection.HTTP_PROXY_AUTH -> {
          nextRequest = route.address.proxyAuthenticator.authenticate(route, response)
            ?: throw IOException("Failed to authenticate with proxy")

@yschimke
Copy link
Collaborator

yschimke commented Feb 4, 2024

Closing as auth is disabled also in JDK since 2016

https://www.oracle.com/java/technologies/javase/8u111-relnotes.html

Please reopen with more info, or a reproduction we can test against.

@yschimke yschimke closed this as completed Feb 4, 2024
@chuckdu21
Copy link
Author

I don't have credentials so... any recommandation ?

@yschimke
Copy link
Collaborator

yschimke commented Feb 5, 2024

https://github.com/yschimke/okhttp/blob/9ee5c7cc9d830fe72b980b6811f8c7e75a91a7e7/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt demonstrates proxy examples for OkHttp vs URLConnection. Including that OkHttp fails on https with HTTP/2.

So you could try forcing HTTP1.1?

  @Test
  @Disabled("https://github.com/square/okhttp/issues/8233")
  fun testOkHttpSecureProxiedHttp2() {

Which I guess confirms what you are seeing. But I don't understand the "Failed to authenticate with proxy"

@chuckdu21
Copy link
Author

I have a kerberos authentication instead ! Just reminded me that
Maybe java.net can access this kerberos authentication
How can i do that with okhttp ?

@chuckdu21
Copy link
Author

I tried forcing HTTP1.1 i got the same Exception.

@yschimke
Copy link
Collaborator

yschimke commented Feb 5, 2024

Ahhh, yep. I forgot. Try this #5978

client.newBuilder()
        .proxyAuthenticator(okhttp3.Authenticator.JAVA_NET_AUTHENTICATOR)
        .build()

@chuckdu21
Copy link
Author

I tried already but same :'(

@yschimke
Copy link
Collaborator

yschimke commented Feb 5, 2024

I'll add an example to that test I linked to.

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

2 participants