1

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

但是我得到的响应总是向我显示一个错误作为答案。我真的不知道问题出在哪里。也许有人可以帮助我。

在我的代码下面:

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

所以我猜,我的标题不完整。但我不明白如何正确设置它。

4

1 回答 1

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();
        }

    }
}
于 2014-05-31T22:10:33.197 回答