简体   繁体   English

如何在Java中调用soap服务?

[英]How do I call the soap service in Java?

I would like to call the soap service that I have implemented externally in Java. 我想调用我在Java中外部实现的soap服务。

However, the following error will continue to occur. 但是,以下错误将继续发生。 'Server did not recognize the value of HTTP Header SOAPAction' “服务器无法识别HTTP标头SOAPAction的值”

Please see what's wrong with my code. 请查看我的代码有什么问题。

test url is ' http://example.com/test.asmx?op=tt ' 测试网址为“ http://example.com/test.asmx?op=tt

** what is soapaction????? **什么是肥皂作用?

import javax.xml.soap.*;

public class SOAPClientSAAJ {

  // SAAJ - SOAP Client Testing
  public static void main (String args[]) {

   String soapEndpointUrl = "http://example.com/test.asmx" ;
     String soapAction = "http://example.com/tt" ;

    callSoapWebService (soapEndpointUrl , soapAction ) ;
  }

private static void createSoapEnvelope (SOAPMessage soapMessage) throws SOAPException {
    SOAPPart soapPart = soapMessage .getSOAPPart () ;

    // SOAP Body
    SOAPBody soapBody = envelope .getBody () ;
    SOAPElement soapBodyElem1 = soapBody .addChildElement ( "element1") ;
    soapBodyElem11 .addTextNode ( "test") ;
    SOAPElement soapBodyElem2 = soapBody .addChildElement ( "element2") ;
    soapBodyElem12 .addTextNode ( "test") ;

}

private static void callSoapWebService (String soapEndpointUrl, String soapAction ) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory . newInstance() ;
        SOAPConnection soapConnection = soapConnectionFactory .createConnection () ;

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

        // Print the SOAP Response
        System .out . println( "Response SOAP Message:" );
        soapResponse .writeTo ( System. out );
        System .out . println() ;

        soapConnection .close () ;
    } catch (Exception e) {
        System .err . println( "\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n") ;
        e .printStackTrace () ;
    }
}

  private static SOAPMessage createSOAPRequest (String soapAction) throws Exception {
    MessageFactory messageFactory = MessageFactory .newInstance () ;
    SOAPMessage soapMessage = messageFactory .createMessage () ;

    createSoapEnvelope (soapMessage ) ;

    MimeHeaders headers = soapMessage .getMimeHeaders () ;
    headers .addHeader ( "SOAPAction", soapAction) ;

    soapMessage .saveChanges () ;

    /* Print the request message, just for debugging purposes */
    System .out . println( "Request SOAP Message:" );
    soapMessage .writeTo ( System. out );
    System .out . println( "\n" );

    return soapMessage ;
  }

}

Is there no WSDL available? 没有可用的WSDL吗? It would be much easier because you can generate a client using eclipse or wsimport. 这样会容易得多,因为您可以使用eclipse或wsimport生成客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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