user3128303
user3128303

Reputation: 747

proxy for the REST

I have a method in which sending XML(byte array) through REST. I set the global properties of a test proxy (CCProxy). In the application CCProxy log I have nothing. What did I do wrong?

url = "https://myurl/api/Storage/Init";
SSLContext sc = createSslContext();
Client client = ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier(new HostnameVerifier() {
    public boolean verify(String s, SSLSession sslSession) {
        return true;
    }
}).build();
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "192.168.11.64");
System.setProperty("http.proxyPort", "808");
System.setProperty("https.proxyHost", "192.168.11.64");
System.setProperty("https.proxyPort", "808");
// client.property("http.proxy.server.uri", "192.168.11.64");
// client.property("http.proxy.server.port", "808");
// client.property("https.proxy.server.uri", "192.168.11.64");
// client.property("https.proxy.server.port", "808");
javax.ws.rs.core.Response response = client.target(url).request(MediaType.APPLICATION_JSON)
    .post(Entity.entity(doc, MediaType.APPLICATION_XML));

setting CCProxy

setting CCProxy

The second question, how to set the proxy only for a particular method? I tried something like this but it did not work.

client.property("http.proxy.server.uri", "192.168.11.64");
client.property("http.proxy.server.port", "808");

Update: After the changes as suggested by CWasp

final SSLContext sslContext = new SSLContextBuilder()
    .loadTrustMaterial(null, (x509CertChain, authType) -> true).build();

HttpClient httpClient = HttpClientBuilder.create()
    .setProxy(new HttpHost("192.168.11.64", 808))
    .setDefaultCredentialsProvider(credProvider).setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
    .setSSLHostnameVerifier(new NoopHostnameVerifier())
    .setSSLContext(sslContext)
    .build();

ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
client = new ResteasyClientBuilder().httpEngine(engine).build();

Upvotes: 1

Views: 2966

Answers (1)

CyberWasp
CyberWasp

Reputation: 361

Try this if proxy authentication required

For Jersey 2.x:

    ClientConfig config = new ClientConfig();
    config.connectorProvider(new ApacheConnectorProvider());
    config.property(ClientProperties.PROXY_URI, "http://" + PROXY_HOST + ":" + PROXY_PORT);
    config.property(ClientProperties.PROXY_USERNAME, PROXY_USER);
    config.property(ClientProperties.PROXY_PASSWORD, PROXY_PASS);
    JerseyClient client = new JerseyClientBuilder()
            .withConfig(config)
            .build();

For Resteasy:

    Credentials credentials = new UsernamePasswordCredentials(PROXY_USER, PROXY_PASS);
    CredentialsProvider credProvider = new BasicCredentialsProvider();
    credProvider.setCredentials(new AuthScope(PROXY_HOST, PROXY_PORT), credentials);
    HttpClient httpClient = HttpClientBuilder.create()
            .setProxy(new HttpHost(PROXY_HOST, PROXY_PORT))
            .setDefaultCredentialsProvider(credProvider)
            .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
            .build();
    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
    ResteasyClient client = new ResteasyClientBuilder()
            .httpEngine(engine)
            .build();

Upvotes: 4

Related Questions