2014-10-10 90 views
1

我是SOAP新手。我在StackOverflow上找到了一个简单的Java肥皂客户端示例here,但是还没有碰到W3C's validator service。这是我曾尝试:使用Java调用W3C的Validator SOAP服务

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 1.2,但错误(我反正)是,响应即将到来回到纯html。

回答

2

问题是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格式,这样,那么你可以做任何你想做的事情。

1

MessageFactory.newInstance()应创建为MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)

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