现在的位置: 首页 > 综合 > 正文

axis 添加 soapHeader 请求

2017年12月08日 ⁄ 综合 ⁄ 共 2793字 ⁄ 字号 评论关闭

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);
 }

}

【上篇】
【下篇】

抱歉!评论已关闭.