//Axis 下 的WebService 客户端接入 简单模版
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.message.SOAPHeaderElement; public class WSUnit2 { public WSUnit2() { } /** * @param args */ public static void main(String[] args) { try { // 服务端的url,需要根据情况更改 String endpointURL = "http://www.xxx.cn/xxxServiceTest/WebService.asmx"; String namespace = "http://www.xxx.cn/"; String methodName = "GetDestCityList"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpointURL); call.setSOAPActionURI(namespace + methodName); call.setOperationName(new QName(namespace, methodName)); call.setUseSOAPAction(true); // 由于需要认证,故需要设置调用的用户名和密码。 SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement( namespace, "StationServiceHeader"); soapHeaderElement.setNamespaceURI(namespace); try { soapHeaderElement.addChildElement("UserName").setValue("xxx"); soapHeaderElement.addChildElement("PassWord").setValue("xxx"); } catch (SOAPException e) { e.printStackTrace(); } call.addHeader(soapHeaderElement); //设置返回类型 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); //参数类型 call.addParameter(new QName(namespace, "start_city"), org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN); call.addParameter(new QName(namespace, "end_city"), org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN); //执行调用 String start_city = "浙江"; String end_city = "hz"; String result = (String) call.invoke(new Object[] { start_city, end_city }); System.out.println("result = " + result); } catch (ServiceException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }