具有代理和Autentification的WS客户端

cos*_*sor 7 java web-services jax-ws webservices-client jax-ws-customization

我知道这不是提出问题的正确方法,但我遇到了问题:

我有一个本地存储的wsdl,我需要创建一个Web服务客户端来调用该Web服务.问题是服务是在防火墙后面,我必须通过代理连接到它,然后我必须验证连接到WS.

我所做的是使用Apache CXF 2.4.6生成WS客户端,然后设置系统范围的代理

System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "10.10.10.10");
System.getProperties().put("https.proxyPort", "8080");
Run Code Online (Sandbox Code Playgroud)

我知道这不是最好的做法,所以请提出一个更好的解决方案,如果有人能给我一个关于如何设置验证的提示我真的很感激它

小智 19

用apache CXF

HelloService hello = new HelloService();
HelloPortType helloPort = cliente.getHelloPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloPort);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getClient().setProxyServer("proxy");
http.getClient().setProxyServerPort(8080);
http.getProxyAuthorization().setUserName("user proxy");
http.getProxyAuthorization().setPassword("password proxy");
Run Code Online (Sandbox Code Playgroud)


jon*_*ckt 6

如果您使用的是Spring Java配置,要使用Apache CXF(3.xx)配置JAX-WS客户端,以下代码将起作用:

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.codecentric.namespace.weatherservice.WeatherService;

@Configuration
public class WeatherServiceConfiguration {

    @Bean
    public WeatherService weatherService() {
        JaxWsProxyFactoryBean jaxWsFactory = new JaxWsProxyFactoryBean();
        jaxWsFactory.setServiceClass(WeatherService.class);
        jaxWsFactory.setAddress("http://yourserviceurl.com/WeatherSoapService_1.0");
        return (WeatherService) jaxWsFactory.create();
    }

    @Bean
    public Client client() {
        Client client = ClientProxy.getClient(weatherService());
        HTTPConduit http = (HTTPConduit) client.getConduit();
        http.getClient().setProxyServer("yourproxy");
        http.getClient().setProxyServerPort(8080); // your proxy-port
        return client;
    }
}
Run Code Online (Sandbox Code Playgroud)