你用wsdl文件生成RPC或者WS的客户端呗

解决方案 »

  1.   

    跨语言了啊,得用wsdl,用dom对象去解析,实现
      

  2.   


    有很多同学问我使用axis调用.net带soapheader的webservice是如何实现的,现在贴出代码 
    .net webservice的soap代码如下,注意第四行: 
    Java代码 
    <?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>  <?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>java代码: 
    Java代码 
    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();   
            }   
        }   
    }  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();
    }
    }
    }
    第27 28两行,NamespaceURI要写两次,如果不知道你的webservice的NamespaceURI是什么,就要wsdl里面去看一下 
      

  3.   

    樓上的這段代碼是特指有帳號和密碼要求的。 普通的WEBSERVICE 沒有些需求,並不適用。看似簡單的一個問題,居然三,四天了,沒人有能力回答?