由于业务上的需要,需要访问第三方提供的webservice接口,但由于公司做了对外访问的限制,不设置代理是不能外网的,如果使用http设置代理访问外网还是比较容易的,但使用cxf有点不知道从哪里入手。网上也有一些零散的信息,现在我整理一下提供参考。
import org.apache.cxf.configuration.security.ProxyAuthorizationPolicy; 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.apache.cxf.transports.http.configuration.HTTPClientPolicy;public class TeacherService { public String getStudents(ReqPosition position){ JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setServiceClass(TeacherWebService.class); factoryBean.setAddress("http://xxx.xxx.xxx.xxx/webservice?wsdl"); TeacherWebService tService = (TeacherWebService )factoryBean.create(); Client client = ClientProxy.getClient(tService); HTTPConduit http = (HTTPConduit) client.getConduit(); HTTPClientPolicy hcp = new HTTPClientPolicy(); hcp.setProxyServer(proxyHost); hcp.setProxyServerPort(proxyport); http.setClient(hcp); ProxyAuthorizationPolicy proxyAuthorization = new ProxyAuthorizationPolicy(); proxyAuthorization.setUserName(proxyUsername); proxyAuthorization.setPassword(proxyPassword); http.setProxyAuthorization(proxyAuthorization); String res = tService.getStudents(); return res; } }
原文:http://www.cnblogs.com/ginponson/p/5259383.html