从JWSDL创建SOAP请求

时间:2010-08-24 20:29:40

标签: java web-services soap wsdl axis

嘿那里,我一直在使用JWSDL来允许我以编程方式使用WSDL文件。我现在想要创建可以发送到服务器的SOAP请求。如何从JWSDL类生成这些请求?有任何想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以这样做:

  

在这里,我创建了一个带有两个参数的示例Web服务   number1和number2。并将响应作为number3(= number1 +   2号)。 Web服务已部署在localhost:8080(tomcat   服务器)

您的答案从此处开始

  

我创建了一个示例java文件...它将两个参数传递给a   SOAP请求中的Web服务,并从Web获取SOAP响应   服务。您可以从WSDL文件中获取参数(在代码中描述),如getCalculation,m,localhost:8080,number1,number2和url。

示例代码:

package SampleJavaWSDLDemo;
  • import javax.xml.soap。*;
  • import java.util。*;
  • import java.net.URL;
  • import javax.xml.transform。*;
  • import javax.xml.transform.stream.StreamResult;
  • import javax.xml.soap.SOAPConnectionFactory;
  • import javax.xml.soap.SOAPConnection;
  • import javax.xml.soap.MessageFactory;
  • import javax.xml.soap.SOAPMessage;
  • import javax.xml.soap.SOAPPart;
  • import javax.xml.soap.SOAPEnvelope;
  • import javax.xml.soap.SOAPBody;
  • import java.net.URL;

公共类SampleJavaWSDLDemo {

public static void main(String[] args) 
{
    try {

    //Create a SOAPMessage
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = soapConnectionFactory.createConnection();
    SOAPFactory soapFactory = SOAPFactory.newInstance();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    SOAPHeader header = message.getSOAPHeader();
    SOAPBody body = message.getSOAPBody();
    header.detachNode();

    Name bodyName = soapFactory.createName("getCalculation", "m", "http://localhost:8080/");
    SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

            //Insert Content
    Name name = envelope.createName("number1");
    SOAPElement symbol = bodyElement.addChildElement(name);
    symbol.addTextNode("10");
    name = envelope.createName("number2");
    symbol = bodyElement.addChildElement(name);
    symbol.addTextNode("20");

            System.out.println("\n Request: \n");
            message.writeTo(System.out);
            System.out.println();

            // Create an endpint point which is either URL or String type
    URL endpoint = new URL("http://localhost:8080/WebServiceName/OperationName");

            //Send a SOAPMessage (request) and then wait for SOAPMessage (response)
    SOAPMessage response = connection.call(message, endpoint);

    // Get the response from the webservice.                
    SOAPBody soapBody = response.getSOAPBody();

    System.out.println("\n Response: \n");
    TransformerFactory transformerfactory = TransformerFactory.newInstance();
    Transformer transformer = transformerfactory.newTransformer();
    Source sourceContent = response.getSOAPPart().getContent();
    StreamResult result = new StreamResult(System.out);
    transformer.transform(sourceContent, result);
    System.out.println();
    String resp = response.getSOAPBody().getElementsByTagName("return").item(0).getFirstChild().getNodeValue();
    System.out.println("Answer is: " + resp);

    connection.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

尝试运行此代码。 它可以为您提供完整的肥皂请求和响应消息。

相关问题