Closed gianlucaUlivo closed 6 years ago
Hm, I have never tested a proxy with authentication. I'll look into this. In the meantime you could use the preparePost
method that was added in 3.1.0 (released this morning). This returns the HttpPost
that you can then use to make the request using the code you just posted.
Can you post an example... 😬
Well, using the code you suggested, it basically boils down to...
HttpPost httpPost = pushService.preparePost(notification);
and then:
HttpHost proxy = new HttpHost("proxy.test.it", 3128);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("proxy.test.it", 3128),
new UsernamePasswordCredentials("username", "passw")
);
CloseableHttpAsyncClient closeableHttpAsyncClient = HttpAsyncClients.custom()
.setProxy(proxy)
.setRoutePlanner(routePlanner)
.setDefaultCredentialsProvider(credsProvider)
.build();
closeableHttpAsyncClient.start();
HttpResponse httpResponse = (HttpResponse) closeableHttpAsyncClient.execute(httpPost, new ClosableCallback(closeableHttpAsyncClient)).get();
For anyone using a proxy that doesn't support UsernamePasswordCredentials
, the Apache Http Client docs cover alternative authentication strategies.
Thanks for the addition! I assume from your reply that this solution solves the original issue posted by @gianlucaUlivo, so I'm closing this.
Well, using the code you suggested, it basically boils down to...
HttpPost httpPost = pushService.preparePost(notification);
and then:
HttpHost proxy = new HttpHost("proxy.test.it", 3128); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("proxy.test.it", 3128), new UsernamePasswordCredentials("username", "passw") ); CloseableHttpAsyncClient closeableHttpAsyncClient = HttpAsyncClients.custom() .setProxy(proxy) .setRoutePlanner(routePlanner) .setDefaultCredentialsProvider(credsProvider) .build(); closeableHttpAsyncClient.start(); HttpResponse httpResponse = (HttpResponse) closeableHttpAsyncClient.execute(httpPost, new ClosableCallback(closeableHttpAsyncClient)).get();
This code will change all http request by proxy or only changed current send notification?
if i configure the JVM parameter with -Dhttp.proxyHost=proxy.test.it -Dhttp.proxyPort=3128 -Dhttp.proxyUser=username -Dhttp.proxyPassword=passw -Dhttps.proxyHost=proxy.test.it -Dhttps.proxyPort=3128 -Dhttps.proxyUser=username -Dhttps.proxyPassword=passw but the autentication doesn't work.
the solution to work is: HttpHost proxy = new HttpHost("proxy.test.it", 3128); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("proxy.test.it", 3128), new UsernamePasswordCredentials("username", "passw")); CloseableHttpAsyncClient closeableHttpAsyncClient = HttpAsyncClients.custom() .setProxy(proxy) .setRoutePlanner(routePlanner) .setDefaultCredentialsProvider(credsProvider) .build();