REST的代理

时间:2016-09-28 07:56:02

标签: rest java-ee wildfly

我有一种通过REST发送XML(字节数组)的方法。我设置了测试代理(CCProxy)的全局属性。在应用程序CCProxy日志中我什么都没有。我做错了什么?

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));

设置CCProxy

setting CCProxy

第二个问题,如何仅为特定方法设置代理?我尝试过类似的东西,但它不起作用。

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

更新:根据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();

1 个答案:

答案 0 :(得分:2)

如果需要代理身份验证,请尝试此操作

对于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();

对于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();