java动态调用webservice

发布时间 2023-12-15 13:17:12作者: 伪装大牛
package com.smp.common;

import serverInfo.ServerInfo;

import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.net.URL;
import java.util.HashMap;

public class DynamicWebService {
    private String url = "http://xxxx:8080/SignalListenServer/SignalListenDelegate?WSDL";
    private String nameSpase = "http://webservice/";
    private String methodName = "getxxx";
    private HashMap<String,String> param;

    /**
     * 获取结果
     *
     * @param methadName 方法名 如getXXX
     * @param htParam
     */
    public DynamicWebService(String methadName, HashMap htParam) {
        this.methodName = methadName;
        this.param = htParam;
        url =  "http://" + ServerInfo.getWebServiceIP() + ":8080/ListenPort?wsdl";
        nameSpase = "http://webservice/";
    }


    public String GetServiceResult() {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server.
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), this.url);

            // Process the SOAP Response
            String s = printSOAPResponse2(soapResponse);
            soapConnection.close();

            return GetValueByTag(s, "return");


        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
            return "";
        }
    }


    public static String GetValueByTag(String xmlStr, String tag) {
        int iStart = xmlStr.indexOf("<" + tag + ">") + 8;
        int iEnd = xmlStr.indexOf("</" + tag + ">");
        return xmlStr.substring(iStart, iEnd);
    }

    /**
     * 入参
     * @return
     * @throws Exception
     */
    private SOAPMessage createSOAPRequest() throws Exception {

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = this.nameSpase;

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("web", serverURI);

        SOAPBody soapBody = envelope.getBody();

        SOAPElement soapBodyElemt = soapBody.addChildElement("web:"+this.methodName);

        for (String key : this.param.keySet()) {
            soapBodyElemt.addChildElement(key).addTextNode(this.param.get(key));
        }

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader(this.methodName, serverURI +this.methodName);

        soapMessage.saveChanges();

        return soapMessage;
    }


    private static String printSOAPResponse2(SOAPMessage soapResponse) throws Exception {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            Source sourceContent = soapResponse.getSOAPPart().getContent();
            StringWriter stringWriter = new StringWriter();
            StreamResult result = new StreamResult(stringWriter);
            transformer.transform(sourceContent, result);
            return stringWriter.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

示例:

 package com.smp.test;


 import com.smp.common.DynamicWebService;
 import java.util.HashMap;


 public class test  {

  public static void main(String args[]) {

    HashMap ht=new HashMap();
    ht.put("arg0","{\"Light\":1,\"ID\":29}");
    ht.put("arg1","192.168.1.106");
    String s = new DynamicWebService("getLight",ht).GetServiceResult();
    
    System.out.println(s);
  }
 }

jar包  saaj.api.jar