2015-03-31 48 views
-1

任何人都可以知道如何打印soap請求和響應xml。在Soap中打印請求和響應xml

請找到下面的代碼。

代碼:

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; 
} 

}

回答

0

我可能是錯的,但是,使用稱爲了SoapUI軟件,您可以:

  • 開始了SoapUI MockService
  • 設置了SoapUI MockService的URL作爲您服務的端點

  • 在了SoapUI,打開其中有您想要呼叫

  • 運行程序(在了SoapUI MockService運行)
  • 讀取請求你的程序中了SoapUI
發送的方法的名稱標籤

希望它可以幫助

編輯:教程這比我的解釋好得多可以在互聯網上找到,如果你是上了SoapUI初學者

+0

@answersseeker:無論是在possilbe日至打印e代碼本身。現在我只有端點URL。我沒有wsdl文件 – bharathi 2015-03-31 16:36:36