简体   繁体   中英

Calling W3C's Validator SOAP Service Using Java

I am new to SOAP. I have found a simple Java soap client example here on StackOverflow, but have not had luck with W3C's validator service . Here is what I have tried:

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

Here is my result:

    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)

I have set my parameter to return SOAP 1.2, but the error (to me anyway) is that the response is coming back as plain html.

The problem is that W3C's validator only returns output in SOAP format. You actually have to send the request to them using HTTP. Try this out:

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

That will return the data to you in SOAP format, so you can then do whatever you want with it.

The MessageFactory.newInstance() should be created as MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL) .

Otherwise you get exceptions while parsing the SOAP response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM