zoukankan      html  css  js  c++  java
  • java axis调用带有soap头(soapheader)的.net webservice

    使用axis调用.net带soapheader的webservice是如何实现的,现在贴出代码

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
      <AuthHeaderCS xmlns="http://tempuri.org/">
          <Username>string</Username>
          <Password>string</Password>
        </AuthHeaderCS>
      </soap:Header>
      <soap:Body>
        <StarTrans xmlns="http://tempuri.org/" />
      </soap:Body>
    </soap:Envelope>
    View Code
    import java.rmi.RemoteException;
    
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ParameterMode;
    import javax.xml.rpc.ServiceException;
    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 aa
    {
        public static void main(String[] args) throws ServiceException, RemoteException
        {
            try
            {
                // 服务端的url,需要根据情况更改。
                String endpointURL = "http://192.168.0.209:7080/DataShareWebService.asmx?wsdl";
                Service service = new Service();
                Call call = (Call) service.createCall();
                call.setTargetEndpointAddress(new java.net.URL(endpointURL));
                call.setSOAPActionURI("http://tempuri.org/" + "StarTrans");
                call.setOperationName(new QName("DataShareWebService", "StarTrans"));// 设置操作的名称。
                // 由于需要认证,故需要设置调用的用户名和密码。
                SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("http://tempuri.org/", "AuthHeaderCS");
                soapHeaderElement.setNamespaceURI("http://tempuri.org/");
                try
                {
                    soapHeaderElement.addChildElement("Username").setValue("admin");
                    soapHeaderElement.addChildElement("Password").setValue("123");
                }
                catch (SOAPException e)
                {
                    e.printStackTrace();
                }
                call.addHeader(soapHeaderElement);
                call.setReturnType(XMLType.XSD_STRING);// 返回的数据类型
                call.addParameter("op1", XMLType.XSD_STRING, ParameterMode.IN);// 参数的类型
                String ret = (String) call.invoke(new Object[] { "11111" });// 执行调用
                System.out.println(ret);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    View Code
  • 相关阅读:
    Spring MVC 处理 Multipart/form-data
    微信/支付宝调用获取用户信息(服务号/小程序)
    springmvc Controller接收前端参数的几种方式总结
    java 远程方法调用(RMI)
    设计模式---结构型模式之适配器模式(Adapter Pattern)
    设计模式----行为型模式之命令模式(Command Pattern)
    设计模式----创建型型模式之单件模式(Singleton pattern)
    设计模式----创建型模式之工厂模式(FactoryPattern)
    java 反射
    设计模式----行为型模式之观察者模式(Observer Pattern)
  • 原文地址:https://www.cnblogs.com/chenghu/p/5612102.html
Copyright © 2011-2022 走看看