肥皂呼叫http://www.w3schools.com/webservices/tempconvert.asmx

时间:2013-10-31 15:13:53

标签: java soap

我已经为上述服务写了一个肥皂客户端。

但我得到的回应总是向我显示错误作为回答。我无法真正找出问题所在。也许有人可以帮助我。

在我的代码下面:

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

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();

        // Get Mime Header
        MimeHeaders mimeHeader = message.getMimeHeaders();
        mimeHeader.addHeader("Host", "www.w3schools.com");
        mimeHeader.addHeader("Content-Type", "text/xml; charset=utf-8");

        mimeHeader.addHeader("SOAPAction",
                "http://tempuri.org/FahrenheitToCelsius");

        // Get soap header
        SOAPHeader header = message.getSOAPHeader();

        // Enter data into header

        // Get soap body
        SOAPBody body = message.getSOAPBody();
        // Enter data into body
        SOAPElement temp = body.addChildElement("FahrenheitToCelsius", "",
                "http://tempuri.org");

        SOAPElement fahr = temp.addChildElement("Fahrenheit");
        fahr.addTextNode("140");

        // print what we are sending
        message.writeTo(System.out);

        SOAPConnection connection = SOAPConnectionFactory.newInstance()
                .createConnection();
        SOAPMessage response = connection.call(message, endpoint);
        connection.close();

        System.out.println("");
        System.out.println("---------------");
        response.writeTo(System.out);

    } catch (SOAPException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

这就是我发送的内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <FahrenheitToCelsius xmlns="http://tempuri.org">
            <Fahrenheit>140</Fahrenheit>
        </FahrenheitToCelsius>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这就是服务要求我发送的内容:

POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/FahrenheitToCelsius"

<?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>
    <FahrenheitToCelsius xmlns="http://tempuri.org/">
      <Fahrenheit>string</Fahrenheit>
    </FahrenheitToCelsius>
  </soap:Body>
</soap:Envelope>

方法本身有效。但是回复说明了一个错误:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">          
    <soap:Body>
        <FahrenheitToCelsiusResponse xmlns="http://tempuri.org/">
            <FahrenheitToCelsiusResult>Error</FahrenheitToCelsiusResult>
        </FahrenheitToCelsiusResponse>
    </soap:Body>
</soap:Envelope>

服务说明可在此处找到:http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius

所以我猜,我有一个不完整的标题。但我不知道如何正确设置它。

1 个答案:

答案 0 :(得分:1)

import java.io.IOException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

public class FahrenheitToCelsius  {

    public static void main(String[] args) throws SOAPException {
        try {
            String endpoint = "http://www.w3schools.com/webservices/tempconvert.asmx";
            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();

            // Get Mime Header
            MimeHeaders mimeHeader = message.getMimeHeaders();
            mimeHeader.addHeader("Host", "www.w3schools.com");
            mimeHeader.addHeader("Content-Type", "text/xml; charset=utf-8");

            mimeHeader.addHeader("SOAPAction",
                    "http://www.w3schools.com/webservices/FahrenheitToCelsius");

            SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
            envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
            // Get soap body
            SOAPBody body = message.getSOAPBody();
            // Enter data into body
            SOAPElement temp = body.addChildElement("FahrenheitToCelsius", "",
                    "http://www.w3schools.com/webservices/");

            SOAPElement fahr = temp.addChildElement("Fahrenheit");
            fahr.addTextNode("140");

            // print what we are sending
            message.writeTo(System.out);

            SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
            SOAPMessage response = connection.call(message, endpoint);
            connection.close();

            System.out.println("");
            System.out.println("---------------");
            response.writeTo(System.out);

        } catch (SOAPException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}