我是SOAP新手。我在StackOverflow上找到了一个简单的Java soap客户端示例here,但是没有幸运W3C's validator service。这是我尝试过的:
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格式返回。
答案 0 :(得分: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 :(得分:1)
MessageFactory.newInstance()
应创建为MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
。
否则,在解析SOAP响应时会出现异常。