调用W3C';使用Java的s验证程序SOAP服务

调用W3C';使用Java的s验证程序SOAP服务,java,web-services,soap,w3c-validation,Java,Web Services,Soap,W3c Validation,我不熟悉肥皂。我在StackOverflow上找到了一个简单的JavaSOAP客户机示例,但在这方面我并不幸运。以下是我尝试过的: import javax.xml.soap.*; public class Soapy { public static String siteToTest = "http://www.example.com"; public static void main(String args[]) throws Exception {

我不熟悉肥皂。我在StackOverflow上找到了一个简单的JavaSOAP客户机示例,但在这方面我并不幸运。以下是我尝试过的:

import javax.xml.soap.*;

public class Soapy
{
    public static String siteToTest = "http://www.example.com";

    public static void main(String args[]) throws Exception
    {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "http://validator.w3.org/check";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        /* Print the response message */
        System.out.println("Response SOAP Message:");
        soapResponse.writeTo(System.out);

        soapConnection.close();
    }

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

        String serverURI = "http://validator.w3c.org/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("m", serverURI);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("Check", "m");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("uri", "m");
        soapBodyElem1.addTextNode(siteToTest);
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("output", "m");
        soapBodyElem2.addTextNode("soap12");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI  + "Check");

        soapMessage.saveChanges();

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

        return soapMessage;
    }
}
以下是我的结果:

    Request SOAP Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://validator.w3c.org/"><SOAP-ENV:Header/><SOAP-ENV:Body><m:Check><m:uri>http://www.example.com</m:uri><m:ouput>soap12</m:output></m:Check></SOAP-ENV:Body></SOAP-ENV:Envelope>
Oct 10, 2014 2:06:56 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType
SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
    at Soapy.main(Soapy.java:20)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:602)
    at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
    ... 1 more

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:602)
    at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
    at Soapy.main(Soapy.java:20)
请求SOAP消息:
http://www.example.comsoap12
2014年10月10日下午2:06:56 com.sun.xml.internal.messaging.saaj.soap.MessageImpl IdentificationContentType
严重:SAAJ0537:无效的内容类型。可能是错误消息而不是SOAP消息
线程“main”com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效内容类型:text/html。这是一条错误消息而不是SOAP响应吗?
位于com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
在Soapy.main(Soapy.java:20)
原因:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效内容类型:text/html。这是一条错误消息而不是SOAP响应吗?
位于com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identificationContentType(MessageImpl.java:602)
位于com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
位于com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
位于com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
... 还有一个
原因:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效的内容类型:text/html。这是一条错误消息而不是SOAP响应吗?
位于com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identificationContentType(MessageImpl.java:602)
位于com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
位于com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
位于com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
在Soapy.main(Soapy.java:20)

我已将参数设置为返回SOAP 1.2,但错误(对我来说)是响应返回为纯html。

问题是W3C的验证程序只返回SOAP格式的输出。实际上,您必须使用HTTP向他们发送请求。试试这个:

import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;

public class Soapy2 {
    public static void main(String args[]) throws Exception {
        // W3C's API works on HTML, so set up an HTML connection
        URL url = new URL("http://validator.w3.org/check?output=soap12&uri=http://www.example.com");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        //Wrap the output from your HTTP connection into an InputStream and feed it into an instance of SOAPMessage
        SOAPMessage response = MessageFactory.newInstance().createMessage(null, connection.getInputStream());

        // Close the connection once the data has been collected
        connection.disconnect();

        //Send the SOAP output to an OutputStream of your choice (e.g. the console)
        response.writeTo(System.out);
    }
}

这将以SOAP格式将数据返回给您,以便您可以对其执行任何操作。

应将
MessageFactory.newInstance()
创建为
MessageFactory.newInstance(SOAPConstants.SOAP\u 1\u 2\u协议)

否则,在解析SOAP响应时会出现异常