使用SOAP使用Java进行Web服务调用

时间:2013-09-27 13:02:06

标签: java web-services soap

我收到了以下示例请求,但我不知道如何实际执行它。

POST /InfoTransit/userservices.asmx HTTP/1.1
Host: 10.0.2.52
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://miz.it/infotransit/GetBusStopsList"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetBusStopsList xmlns="http://miz.it/infotransit">
      <auth>
        <user>..of course, I have this..</user>
        <password>..and this..</password>
      </auth>
    </GetBusStopsList>
  </soap:Body>
</soap:Envelope>

我曾尝试编写Java客户端......

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

    /**
     * Starting point for the SAAJ - SOAP Client Testing
     */
    public static void main(String args[]) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "http://miz.it/InfoTransit/userservices.asmx";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://miz.it/infotransit/GetBusStopsList";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement getBusStopsList = soapBody.addChildElement("GetBusStopsList xmlns='http://miz.it/infotransit'");
        SOAPElement auth = getBusStopsList.addChildElement("auth");
        SOAPElement user = auth.addChildElement("user");
        user.addTextNode(" .... ");
        SOAPElement password = auth.addChildElement("password");
        password.addTextNode(" ... ");


        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI);
        headers.addHeader("Content-Type:", "text/xml; charset=uft-8");

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

    /**
     * Method used to print the SOAP Response
     */
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

}

然而,我得到连接超时。此外,输出SOAP消息的包络不完全匹配示例请求的包络。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

客户有问题吗?或者它是服务提供商吗?

1 个答案:

答案 0 :(得分:0)

您可以在代码中尝试此操作:

SOAPElement getBusStopsList = soapBody.addChildElement("GetBusStopsList", "", "http://miz.it/infotransit");

编辑getBusStopsList中的第一个字母是小写字母。所以,请尝试GetBusStopsList

这是新代码:

SOAPElement GetBusStopsList = soapBody.addChildElement("GetBusStopsList", "", "http://miz.it/infotransit");
相关问题