webService原生调用

以下为调用代码

//类service需要导的包

import org.apache.axis.client.Service;
Service service = new Service();
Call call = service.createCall();
//需要访问的webservice地址http://ip:端口号/webservice发布地址?wsdl
call.setTargetEndpointAddress("http://ip:端口号/webservice发布地址?wsdl");
//QName参数 1,webservice的命名空间 2,需要访问的方法名
call.setOperationName(new QName("http://xxxxx/","xxx"));

//参数,对应要访问的方法参数,参数名字要和方法中参数一样
call.addParameter("aaa", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("bbb", XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter("ccc", XMLType.XSD_STRING,ParameterMode.IN);

System.out.println("开始接口调用");
//调用,返回的是xml结构的
Object o = call.invoke(new Object[]{"fdfds","eee","ggggg"});
System.out.println("获得返回值" + o);

webservice的命名空间,可以通过对方给的路径来获取

在浏览器输入地址http://ip:端口号/webservice发布地址?wsdl

红框中为webService的命名空间!

 

 

//另一种webservice请求方式,增加表头

 

 

发送webservice代码

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
String address = ip+webservice地址?wsdl";
org.apache.cxf.endpoint.Client client = dcf.createClient(address);
log.info("拼接的ip地址是" + address);
//增加表头的方法
client.getOutInterceptors().add(new ClientAuthInterceptor(“需要的用户名”, “需要的密码”));
//body json数据
Object[] obj = client.invoke("commonRequest", “json数据”);
Object object = null;
if (null != obj) {
    object = obj[0];
}
if (null == object) {
   
} else {
    //得到返回数据
    String xmlString = object.toString();
    log.info("返回数据成功!");
    log.info(xmlString);
}

 

 

//发送表头的类

package com.ptpec.ims.service.card;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 
 */
public class ClientAuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    private String userName;
    private String passWord;
    
    
    public ClientAuthInterceptor(String userName, String passWord) {
        //准备发送阶段
        super(Phase.PREPARE_SEND);
        this.userName = userName;
        this.passWord = passWord;
  
    }
    
    
    @Override
    public void handleMessage(SoapMessage message) throws Fault {
       
        List<Header> headers = message.getHeaders();
           Document doc = DOMUtils.createDocument();
        Element auth = doc.createElement("AuthenticationToken");
        Element username = doc.createElement("Username");
        username.setTextContent(userName);
        Element password = doc.createElement("Password");
        password.setTextContent(passWord);
        auth.appendChild(username);
        auth.appendChild(password);
        headers.add(new Header(new QName(""), auth));
        
        System.out.println("------------------"+headers);
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值