我是 Java 中 SOAP WebService 开发的新手,我遇到以下问题。
我有一个公开2个方法的web服务,第一个(更简单)已经实现(由另一个人)并命名为 getVersion(),并且在SoapUI中有以下请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:getVersion/>
</soapenv:Body>
</soapenv:Envelope>
然后我有一个Java项目来执行对webservice的调用,在类中我有以前的webservice方法的followin方法:
public String getVersion(){ java.net.URL url = null; java.net.URLConnection conn = null;
java.io.BufferedReader rd = null;
String soapResponse = "";
String risultato = "";
// SOAP ENVELOP for the request:
String soapRequest;
soapRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" + "<soapenv:Header/>" + "<soapenv:Body> " + "<tem:getVersion/>" + "</soapenv:Body>" + "</soapenv:Envelope>";
try {
// Try to open a connection
url = new java.net.URL(_webServiceUrl);
conn = url.openConnection();
// Set the necessary header fields
conn.setRequestProperty("SOAPAction", "http://tempuri.org/IMyService/getVersion");
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setDoOutput(true);
// Send the request:
java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(conn.getOutputStream());
wr.write(soapRequest);
wr.flush();
// Read the response
rd = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
// Put the entire response into the soapResponse variable:
String line;
while ((line = rd.readLine()) != null) {
//System.out.println(line);
soapResponse = soapResponse + line + System.getProperty("line.separator");
}
rd.close();
// elaboro la risposta
SAXBuilder builder = new SAXBuilder();
org.jdom.Document documentXML = null;
documentXML = builder.build(new StringReader(soapResponse));
XPath xPath;
Element objectElement;
//xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult");
xPath = XPath.newInstance("s:Envelope/s:Body");
xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
objectElement = (Element) xPath.selectSingleNode(documentXML);
if (objectElement != null) {
risultato = objectElement.getValue();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return risultato;
}
现在我必须复制相同的东西,为第二个方法创建一个新方法(在调用ws方法的Java项目中)(这更复杂,因为,与前一个方法不同,需要传递一些参数)和我对此有一些疑问。
所以情况如下:在SoapUI中有一个名为 getConfigSettings 的WS方法,其请求是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:getConfigSettings>
<!--Optional:-->
<tem:login>?</tem:login>
<!--Optional:-->
<tem:password>?</tem:password>
<!--Optional:-->
<tem:ipAddress>?</tem:ipAddress>
<!--Optional:-->
<tem:clientVersion>?</tem:clientVersion>
<!--Optional:-->
<tem:lastUpdateTime>?</tem:lastUpdateTime>
</tem:getConfigSettings>
</soapenv:Body>
</soapenv:Envelope>
As you can see it requires some parameters (in SoapUi I have to replace characters with the correct parameter value)
所以,在Java项目中,我创建了以下方法来执行此调用,为我的请求创建 SOAP Envelop (但我对它的正确性有很多怀疑)
public String authentication(String login, String password, String ipAddress, String clientVersion) {
java.net.URL url = null;
java.net.URLConnection conn = null;
java.io.BufferedReader rd = null;
String myResponse = "";
String soapXml;
// SOAP ENVELOP for the request:
//soapXml = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> " + "<s:Header>" + "<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IMyService/getVersion</Action>" + "</s:Header>" + "<s:Body>" + "<getVersion xmlns=\"http://tempuri.org/\" />" + "</s:Body>" + "</s:Envelope>";
soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body> "
+ "<tem:login>" + login + "</tem:login>"
+ "<tem:password>" + password + "</tem:password>"
+ "<tem:ipAddress>" + ipAddress + "</tem:ipAddress>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
........................................................
........................................................
........................................................
DO SOME OTHER STUFF
........................................................
........................................................
........................................................
}
正如您所看到的,我为我的请求创建了这个SOAP Envelop,其中我插入了作为方法的输入参数recived的参数:
soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body> "
+ "<tem:login>" + login + "</tem:login>"
+ "<tem:password>" + password + "</tem:password>"
+ "<tem:ipAddress>" + ipAddress + "</tem:ipAddress>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
这是正确的还是我采取了错误的解决方案?一些建议?
TNX
安德烈
答案 0 :(得分:0)
您的方法看起来很直接,但您需要XML-escape您正在插入的字符串。
否则,如果字符串包含<
等保留的XML字符,则接收方可能无法解析您的请求。只需考虑使用</tem:login>
作为密码的人:)
Guava或Apache commons等库包含XML转发器,请参阅此线程以获取指针: Best way to encode text data for XML in Java?
或者,你可以包括你自己的:
public static String xmlEscape(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sb.length(); i++) {
char c = s.charAt(i);
if ("<&\">'".indexOf(c) != -1) {
sb.append("&#" + ((int) c) + ";");
} else {
sb.append(c);
}
}
return sb.toString();
}
所以你的填充代码看起来类似于:
soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body> "
+ "<tem:login>" + xmlEscape(login) + "</tem:login>"
+ "<tem:password>" + xmlEscape(password) + "</tem:password>"
P.S。切勿以明文形式通过网络发送登录数据!您可能希望使用https而不是http作为您的服务。