简体   繁体   中英

print request and response xml in Soap

Can anybody know how to print the soap request and response xml.

Please find the code below.

Code:

  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Vector;

  import org.apache.soap.Constants;
  import org.apache.soap.Fault;
  import org.apache.soap.SOAPException;
  import org.apache.soap.encoding.SOAPMappingRegistry;
  import org.apache.soap.rpc.Call;
  import org.apache.soap.rpc.Parameter;
  import org.apache.soap.rpc.Response;
  import org.apache.soap.transport.http.SOAPHTTPConnection;


   public class SPMyBenefitsService {
      private Call call;
      private URL url = null;
      private java.lang.reflect.Method setTcpNoDelayMethod;
      private org.w3c.dom.Element rslts = null;
      private ArrayList myBenefitsProducts = new ArrayList();

     public static void main(String arg[]){
        System.out.println("Test");//105843
        SPMyBenefitsService service = new SPMyBenefitsService(9258347, "");
}


public SPMyBenefitsService(int group, String sessKey) {
    System.out.println("Intializing starts..");

    //IBwLogContext ctx = SPUtility.getBwLogContext(sessKey);
    try {
        setTcpNoDelayMethod = SOAPHTTPConnection.class.getMethod("setTcpNoDelay", new Class[] { Boolean.class });
        } catch (Exception e) {
    }


    call = createCall();
    try {

        rslts = getProductList(group,"");
        loadMyBenefitsData(rslts,"");

    } catch (SOAPException ex) {

        System.out.println("SPMetLinkService SOAP Exception = " + ex.toString());

    }

}

private final URL getURL(String ctx) {


    try {
        String tul ="http://****.com/MyBenefits/webservices";
        url = new URL(tul);

    } catch (Exception Ex) {

        System.out.println("exceptioon"+ Ex);

        url = null;
    }

    return url;
}
public org.w3c.dom.Element getProductList(int grpnum, String ctx) throws SOAPException {
    String targetObjectURI = "urn:com.metlife.us.ins.mybenefits.webservices.portal.PortalBusinessObjectProviderService";
    String SOAPActionURI = "";

    if (getURL(ctx) == null) {
        throw new SOAPException(
            Constants.FAULT_CODE_CLIENT,
            "A URL must be specified via PortalBusinessObjectProviderServiceProxy.setEndPoint(URL).");
    }
    System.out.println("test:::"+Constants.NS_URI_LITERAL_XML);
    System.out.println("NS_URI_SOAP_ENC:::"+ Constants.NS_URI_SOAP_ENC);
    call.setMethodName("getProductList");   
    call.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);
    call.setTargetObjectURI(targetObjectURI);
    Vector<Parameter> params = new Vector<Parameter>();
    System.out.println("grpnum"+grpnum);
    Parameter grpnumParam = new Parameter("grpnum", int.class, new Integer(grpnum), Constants.NS_URI_SOAP_ENC);
    params.addElement(grpnumParam);
    call.setParams(params);

    System.out.println("fff--?"+call.getParams());
    long st = System.currentTimeMillis();
    System.out.println("Before call - " + st + " milli seconds");

    Response resp = call.invoke(getURL(ctx),SOAPActionURI);
    long et = System.currentTimeMillis();

    System.out.println("After call - " + et + " milli seconds");
    System.out.println("Diff Mybenefit - " + (et-st) + " milli seconds");
    System.out.println("Diff Mybenefit - " + (et-st) + " milli seconds" );
    //Check the response.
    if (resp.generatedFault()) {
        Fault fault = resp.getFault();
        call.setFullTargetObjectURI(targetObjectURI);
        throw new SOAPException(fault.getFaultCode(), fault.getFaultString());
    } else {
        Parameter refValue = resp.getReturnValue();
        System.out.println("resp"+refValue.getValue());


        File file = new File("H:\\filename.txt");

         try {
         if (!file.exists()) { 
              file.createNewFile(); 
         }

          FileWriter fw = new FileWriter(
          file.getAbsoluteFile()); 
          BufferedWriter bw = new BufferedWriter(fw);
            bw.write(resp.toString());
              bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return ((org.w3c.dom.Element) refValue.getValue());
    }
}
private boolean loadMyBenefitsData(org.w3c.dom.Element doc,String ctx) {

    System.out.println("doc"+doc.toString());

    /*IXml idoc = XmlUtil.create(doc);


    IXml child = idoc.getFirstChildElement();*/

    /*StringBuffer sb = null;
    while ((child != null) && child.getName().equalsIgnoreCase("Product")) {
        if (child.getText("Status").equalsIgnoreCase("Approved")) {
            sb = new StringBuffer();
            sb.append(child.getAttribute("productCode")).append("\011");
            sb.append(child.getText("ShortName")).append("\011");
            sb.append(child.getText("Status")).append("\011");
            sb.append(child.getText("LiveDate")).append("\011");
            myBenefitsProducts.add(sb.toString());
        }
        child = child.getNextSiblingElement();
    }*/

    return true;
}

protected Call createCall() {
    SOAPHTTPConnection soapHTTPConnection = new SOAPHTTPConnection();
    soapHTTPConnection.setTimeout(Integer.parseInt("90000"));   
    if (setTcpNoDelayMethod != null) {
        try {
            setTcpNoDelayMethod.invoke(soapHTTPConnection, new Object[] { Boolean.TRUE });
        } catch (Exception ex) {
        }
    }
    Call call = new Call();
    call.setSOAPTransport(soapHTTPConnection);
    SOAPMappingRegistry smr = call.getSOAPMappingRegistry();
    return call;
}

}

I may be wrong but, using a software called SoapUI, you can:

  • Start a SoapUI MockService
  • Set SoapUI MockService's URL as an endpoint of your service

  • In SoapUI, open the tab which has the name of the method you want to call

  • Run your program (while SoapUI MockService is running)
  • Read the request sent by your program in SoapUI

Hope it helps

edit: Tutorials which are much better than my explanations can be found on the internet if you are a beginner on SoapUI

For a given SOAPMessage you can do the following:

        SOAPMessage soapMessage = ...
        try {
            soapMessage.writeTo(System.out);
        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM