【发布时间】:2019-04-17 07:58:41
【问题描述】:
我有一个 Java 服务器应用程序,它对另一个远程服务器进行一些 Soap 调用。
我已将我的 Java 服务器应用程序放在带有 tomcat 8.5 的远程服务器上。当我进行 Soap 调用时,它需要双倍的时间,而在我的本地机器上使用 Eclipse 上的 tomcat 8.5 会更快。
你知道为什么吗?
这是我的肥皂客户端的代码:
public class SoapHelper {
public String server = "";
public String username = "";
public String password = "";
public String session = ""; // this is the session id returned by the server upon successful login
private SOAPConnection con = null;
private MessageFactory mf = null;
public String service = "";
public String method = "";
public String request = ""; // this is what we send to the server
public String response = ""; // this is what the server return to us
public SoapHelper(String server) {
this.server = server;
}
private String getURI() {
return "https://" + this.server + this.session;
}
private SOAPMessage makeMessage(String nodeName, String xmlStr) throws Exception {
SOAPMessage message = this.mf.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/1999/XMLSchema-instance");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/1999/XMLSchema");
SOAPBody body = envelope.getBody();
SOAPElement element = body.addChildElement(envelope.createName("ns1:" + this.method));
element.addAttribute(envelope.createName("xmlns:ns1"), "urn:" + this.service);
element.addAttribute(envelope.createName("ns1"), "http://schemas.xmlsoap.org/soap/encoding");
SOAPElement ele2 = element.addChildElement(envelope.createName(nodeName));
ele2.addAttribute(envelope.createName("xsi:type"), "xsd:string");
ele2.addTextNode(xmlStr);
message.saveChanges();
return message;
}
private void doConnect()
{
try {
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
this.con = conFactory.createConnection();
this.mf = MessageFactory.newInstance();
} catch(Exception e) {}
}
public boolean doRequest(String service, String method, String xml)
{
this.service = service;
this.method = method;
this.request = "";
this.request = xml;
try {
URL endpoint = new URL(this.getURI());
SOAPMessage message = this.makeMessage("msgstr", this.request);
SOAPMessage retval = this.con.call(message, endpoint);
//extraction du XML en String lisible du message SOAP
this.response = extractXML(retval);
} catch (Exception e) {
this.response = e.getMessage();
}
return true;
}
private String extractXML(SOAPMessage message) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
String returnxml = new String(out.toByteArray());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(returnxml)));
Element root = document.getDocumentElement();
Node msg = root.getLastChild();
return msg.getTextContent();
}
private String getSession() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(this.response)));
Element root = document.getDocumentElement();
return root.getAttribute("sessionid");
}
public void authenticate(String username, String password){
this.username = username;
this.password = password;
try {
String xml="<Message messageid='0'><Entity name='REF_LOGIN'>";
xml+="<Property name='login_cd' value='" + this.username + "' type='string'/>";
xml+="<Property name='password' value='" + this.password + "' type='string'/>";
xml+="<Property name='machine_name' value='" + getMachineName() + "' type='string'/>";
xml+="</Entity></Message>";
doConnect();
doRequest("Login","Authenticate",xml);
this.session = this.getSession();
}catch(Exception e)
{
this.session = e.getMessage();
}
}
【问题讨论】:
-
你能提供一些代码吗?再具体一点?
-
什么类型的代码?我的肥皂客户?