模拟soapui调用webservice

参考别人写的,自己模拟调用成功:
	
	public static void main(String[] args) throws Exception {
		// {*} * 为图片中的数字
	    String ns = "http://alms.acconsys.com/component8y/";  // {1}
        String wsdlUrl = "http://localhost:8080/alms/soa/Component8yService?wsdl";  // {2}
        //1、创建服务(Service)  
        URL url = new URL(wsdlUrl);  
		QName sname = new QName(ns, "Component8y_Service"); // {3}
		Service service = Service.create(url, sname);
	                      
        //2、创建Dispatch  
        Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns, "Component8y_Port"), SOAPMessage.class, Service.Mode.MESSAGE); // {4}  
                      
        //3、创建SOAPMessage  
        SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();  
        SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();  
        SOAPBody body = envelope.getBody();  
                      
        //4、创建QName来指定消息中传递数据  
//        QName ename = new QName(ns, "ZykSynchRequest", "com");//<nn:add xmlns="xx"/>  // {5}
        QName ename = new QName(ns, "ZykSynchRequest");//<nn:add xmlns="xx"/>  // {5}
        SOAPBodyElement ele = body.addBodyElement(ename);  
        
        // 传递参数  
        // {6}
        ele.addChildElement("fileName").setValue("path");    
        msg.writeTo(System.out);  
        System.out.println("\n invoking.....");  
                              
        //5、通过Dispatch传递消息,会返回响应消息  
        SOAPMessage response = dispatch.invoke(msg);  
        response.writeTo(System.out);  
        System.out.println();  
                      
        //6、响应消息处理,将响应的消息转换为dom对象  
        Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();  
        String str = doc.getElementsByTagName("result").item(0).getTextContent();  // {7}
        System.out.println(str);  
	}


猜你喜欢

转载自tianqiushi.iteye.com/blog/2287191