使用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>
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(); } } }