java - Soap 调用 http ://www. w3schools.com/webservices/tempconvert.asmx

标签 java soap

我已经为上述服务编写了一个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

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

最佳答案

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

    }
}

关于java - Soap 调用 http ://www. w3schools.com/webservices/tempconvert.asmx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710248/

相关文章:

java - 在 Eclipse 3.6.1 上安装 AspectJ 时遇到问题

java - net.schmizz.sshj.transport.TransportException : Unable to reach a settlement

php - 通过 SOAP 检索数据并将其发送到 MYSQL 数据库

Soap 请求对象为 null - spring ws

java - 强制重试特定的 http 状态代码

java - 返回内部没有值的java数组

ruby - 使用 Savon 测试本地 Web 服务

xml - eBay NVP api 返回失败

java - 如何在网站中创建即时通知系统

php - 在 __soapCall 之前获取 SOAP XML?