由于业务上的需要,需要访问第三方提供的webservice接口,但由于公司做了对外访问的限制,不设置代理是不能外网的,如果使用http设置代理访问外网还是比较容易的,但使用cxf有点不知道从哪里入手。网上也有一些零散的信息,现在我整理一下提供参考。

1、JaxWsProxyFactoryBean设置代理

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(StudentCard card){
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(card); return res;
}
}

2、使用JaxWsDynamicClientFactory设置代理

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import com.thoughtworks.xstream.XStream; public class TeacherService { public String getStudents(StudentCard card)throws Exception{
     System.setProperty("http.proxyHost","http.proxy.host");
System.setProperty("http.proxyPort", "http.proxy.port");
Authenticator.setDefault(new MyAuthenticator("username", "password"))); JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); //url为调用webService的wsdl地址
Client client = dcf.createClient("http://xxx.xxxx.xxx.xxx:8080/webservice?wsdl");
//namespace是命名空间,methodName是方法名
QName name=new QName("http://www.xxxx.com","getStudents");
    XStream xstream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));
Object[] objects = client.invoke(name,xstream.toXML(card)); String res = "";
if(objects != null && objects.length != 0){
res = objects[0].toString();
} return res;
  }

  static class MyAuthenticator extends Authenticator {
    private String username, password;

    public MyAuthenticator(String username, String password) {
      this.username = username;
      this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password.toCharArray());
    }
  }

}

cxf设置代理访问webservice接口的更多相关文章

  1. WebService---Android中访问WebService接口的方法

     最近公司有个项目需要从Android平台访问WebService接口,实现向发布的函数传递对象.在网上找了一些资料,发现使用ksoap2可以调用WebService传递对象.   需要引入ksoap ...

  2. 十九、.net core使用SoapCore开发webservice接口,以及使用HttpClientFactory动态访问webservice接口

    使用SoapCore实现在.net core平台下开发webservice:以及使用HttpClientFactory动态访问webservice. 首先,需要在包项目下面引用SoapCore: 然后 ...

  3. CXF集成spring做webservice接口

    一 . cxf 的jar包 1.cxf-2.3.3.jar 2.wsdl4j-1.6.2.jar 3.wss4j-1.5.11.jar 4.wstx-asl-3.2.0.jar 5.XmlSchema ...

  4. 浏览器 Proxy SwitchyOmega 插件设置代理访问内网服务器

    使用Proxy SwitchyOmega 插件通过代理 直接访问到内网网站 一.使用场景 如下图所示,如果在电脑的网络设置中开启代理,每次更换代理就需要进入这里设置改变代理.且我们可能回需求到两个网页 ...

  5. Java 通过代理测试webService接口

    以下代码针是针对电脑是通过代理上网的情况时使用: System.getProperties().put("http.proxyHost", "111.111.11.1&q ...

  6. [python]通过urllib2设置代理访问网址

    #!/usr/bin/env pythonimport urllib2 # change followings before useuser = 'foo'passwd = 'bar'proxyser ...

  7. emacs设置代理访问插件仓库

    下面这个配置适合ss (setq url-gateway-method 'socks)(setq socks-server '("Default server" "127 ...

  8. 『动善时』JMeter基础 — 50、使用JMeter测试WebService接口

    目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...

  9. 『动善时』JMeter基础 — 51、使用JMeter测试WebService接口

    目录 1.什么是WebService 2.WebService和SOAP的关系 3.什么是WSDL 4.测试WebService接口前的准备 (1)如何判断是WebService接口 (2)如何获取W ...

随机推荐

  1. xCode5 在ios7模拟器中出现__cxa_throw _pthread_exit错误

    xCode5 在ios7模拟器中出现__cxa_throw _pthread_exit错误 2013年10月28日 ⁄ 综合 ⁄ 共 233字 ⁄ 字号 小 中 大 ⁄ 评论关闭   在项目中用模拟器 ...

  2. iOS开发之功能模块--计算高度Demo探究手稿

    本篇记录关于计算文本高度和Label高度的代码,以备后期再探究: 首先是YouXianMing老师的工具类别: NSString+LabelWidthAndHeight.h // // NSStrin ...

  3. SQL Server下载安装

    参考下载http://www.orsoon.com/Soft/148976.html 安装教程 解压压缩文件,得到安装程序,运行安装程序(如下图) 2..点击左侧的"安装",选择& ...

  4. SQL Server 复制快照执行错误 错误代码 14068

    问题描述: 使用基于快照初始化的事务复制,在上次发布的时候,添加项,执行快照agent,报错,错误信息: Error messages:Message: The subscription status ...

  5. 查询表结构sql

    THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号, col.name AS 列名, ISNULL(ep.value, N'') AS 列说明, t ...

  6. 多重共线性的解决方法之——岭回归与LASSO

          多元线性回归模型 的最小二乘估计结果为 如果存在较强的共线性,即 中各列向量之间存在较强的相关性,会导致的从而引起对角线上的 值很大 并且不一样的样本也会导致参数估计值变化非常大.即参数估 ...

  7. Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理8

    接下来做的是对页面的增删改查与页面与页面按钮之间的联系.先上代码和页面效果 using AuthorDesign.Web.App_Start.Common; using System; using S ...

  8. 全透明Activity

    第一步:在/res/values/styles里面添加如下代码: <style name="translucent"> <item name="andr ...

  9. C#如何使用Soap协议调用WebService?

    WebService是什么?它的作用? WebService是一个平台独立.低耦合的.自包含的.基于可编程的可使用xml描述.调用的web应用程序,用于开发分布式的交互式的应用程序. Soap是什么? ...

  10. ios合并静态库

    lipo -create SQY/iOS/iphoneos/libGamePlusAPI.a SQY/iOS/iphonesimulator/libGamePlusAPI.a -output SQY/ ...