我对 SOAP 很陌生,所以在网上研究了一些程序,这是我想出的,但我得到一个空响应,一定是一些愚蠢的事情,但几乎不需要帮助
请在下面查看我的代码和输出。谢谢
代码
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
public class AtomicNumber {
public static void main(String[] args) {
try {
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage smsg = mf.createMessage();
SOAPHeader shead = smsg.getSOAPHeader();
SOAPBody sbody = smsg.getSOAPBody();
shead.detachNode();
QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber", "web");
SOAPBodyElement bodyElement = sbody.addBodyElement(bodyName);
QName qn = new QName("ElementName");
SOAPElement quotation = bodyElement.addChildElement(qn);
quotation.addTextNode("iron");
System.out.println("\n Soap Request:\n");
smsg.writeTo(System.out);
System.out.println();
URL endpoint = new URL("http://www.webservicex.net/periodictable.asmx");
SOAPMessage response = connection.call(smsg, endpoint);
System.out.println("\n Soap Response:\n");
System.out.println(response.getContentDescription());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我的输出
Soap Request:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><web:GetAtomicNumber xmlns:web="http://www.webserviceX.NET"><ElementName>sodium</ElementName></web:GetAtomicNumber></SOAP-ENV:Body></SOAP-ENV:Envelope>
Soap Response:
null
更新
这就是我得到的(例外)
<faultcode>soap:Server</faultcode>
<faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: Procedure or function 'GetAtomicNumber' expects parameter '@ElementName', which was not supplied.
at WebServicex.periodictable.GetAtomicNumber(String ElementName)
--- End of inner exception stack trace ---</faultstring>