java - 如何在 Java 中调用 soap 服务?

标签 java http soap asmx

我想调用我在外部用 Java 实现的 soap 服务。

但是,会继续出现下面的错误。 '服务器无法识别 HTTP header SOAPAction 的值'

请看看我的代码有什么问题。

测试网址是'http://example.com/test.asmx?op=tt '

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

}

最佳答案

是否没有可用的 WSDL?这会容易得多,因为您可以使用 eclipse 或 wsimport 生成客户端。

关于java - 如何在 Java 中调用 soap 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46843152/

相关文章:

http - 如何在 curl 配置文件中设置自定义 header

angularjs - Angular $http 状态 (-1)

java - CXF 自动生成失败,maven 中出现 "Duplicated option: frontend"?

sockets - HTTP/SOAP 数据包大小

java - Spring:bean 作为 Java Config 中的构造函数参数

java - SQL 命令未正确以 select 结束

mysql - Heroku 有没有办法连接到远程 MySQL 数据库?

c# - 发送和接收 SOAP 消息

java - 在我的 Java 随机数猜谜游戏中添加 "Play Again?"功能

java - 如何设计极其高效的功能