java axis soapheader_axis 添加 soapHeader 请求 | 学步园

1、axis1.4

import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.soap.SOAPElement;

import javax.xml.soap.SOAPException;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.XMLType;

import org.apache.axis.message.SOAPHeaderElement;

public class TestWebservice2 {

private final static String endpoint = "URL";

public static void main(String[] args) throws Exception {

// 产生 SOAP Header

SOAPHeaderElement cpHeader = new SOAPHeaderElement(

"urn:BackstageInterface", "auth");

cpHeader.setPrefix("");

cpHeader.setMustUnderstand(true);

SOAPElement ele = cpHeader.addChildElement("authcode");

ele.addTextNode("password");

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(endpoint));

call.setOperationName(new QName("urn:BackstageInterface", "sendSMS"));

call.addParameter("title", XMLType.XSD_STRING, ParameterMode.IN);

call.addParameter("body", XMLType.XSD_STRING, ParameterMode.IN);

call.addParameter("fullName", XMLType.XSD_STRING, ParameterMode.IN);

call.setReturnType(XMLType.XSD_STRING);

// 设定 Header

call.addHeader(cpHeader);

System.out.println(call.invoke(new Object[]{"test ok","张潮","羊姬"}));

}

}

2、axis2

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.soap.SOAPFactory;

import org.apache.axiom.soap.SOAPHeaderBlock;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

import org.apache.axis2.saaj.SOAPHeaderImpl;

public class TestWebservice {

/**

* @param args

*/

public static void main(String[] args) throws Exception {

RPCServiceClient serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();

// 这一步指定了该服务的提供地址

EndpointReference targetEPR = new EndpointReference(

"URL");

// 将option绑定到该服务地址

options.setTo(targetEPR);

OMFactory fac = OMAbstractFactory.getOMFactory();

OMNamespace omNs = fac.createOMNamespace("urn:BackstageInterface", "auth");

OMElement header = fac.createOMElement("auth", omNs);

OMElement authcode = fac.createOMElement("authcode", omNs);

authcode.setText("password");

header.addChild(authcode);

QName opAdd = new QName("urn:BackstageInterface", "sendSMS");

// 设置返回值类型

Class[] returnTypes = new Class[] { String.class };

// 设置调用的参数

Object[] opAddArgs = new Object[] { "test test", "test", "羊姬" };

// 调用服务,获得返回值

serviceClient.addHeader(header);

Object[] response = serviceClient.invokeBlocking(opAdd, opAddArgs,

returnTypes);

String res = (String) response[0];

if (res == null) {

System.out.println("wrong");

return;

}

System.out.println(res);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值