【发布时间】:2018-03-07 17:01:57
【问题描述】:
我必须制作一个java 模拟器来调用java 中的多个soap 请求。 WSDL 中有 4 个 Soap 请求。我可以通过在 java 中对 XML 进行硬编码来调用一个请求,但我想要一种调用肥皂服务的动态方法。 在网上没有找到任何东西。
package sisII.JNDI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.soap.*;
public class MessageConnector {
public String callService (String IDNumber) throws MalformedURLException, IOException, SOAPException {
String urlString = "http://wsdl_url";
URL urlForInfWebSvc = new URL(urlString);
URLConnection UrlConnInfWebSvc = urlForInfWebSvc.openConnection();
HttpURLConnection httpUrlConnInfWebSvc = (HttpURLConnection) UrlConnInfWebSvc;
httpUrlConnInfWebSvc.setDoOutput(true);
httpUrlConnInfWebSvc.setDoInput(true);
httpUrlConnInfWebSvc.setAllowUserInteraction(true);
httpUrlConnInfWebSvc.setRequestMethod("POST");
httpUrlConnInfWebSvc.setRequestProperty("Host", "localhost");
httpUrlConnInfWebSvc.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
httpUrlConnInfWebSvc.addRequestProperty(header_credentials);
OutputStreamWriter infWebSvcReqWriter = new OutputStreamWriter(httpUrlConnInfWebSvc.getOutputStream());
String infWebSvcRequestMessage =
" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:req=\"http://def" xmlns:com=\"http://xyz" xmlns:aler=\"http://abc"> " +
" <soapenv:Header/>" +
"<soapenv:Body>" +
......
"</soapenv:Body>" +
"</soapenv:Envelope>" ;
infWebSvcReqWriter.write(infWebSvcRequestMessage);
infWebSvcReqWriter.flush();
BufferedReader infWebSvcReplyReader = new BufferedReader(new InputStreamReader(httpUrlConnInfWebSvc.getInputStream()));
String line;
String infWebSvcReplyString = "";
while ((line = infWebSvcReplyReader.readLine()) != null) {
infWebSvcReplyString = infWebSvcReplyString.concat(line);
}
infWebSvcReqWriter.close();
infWebSvcReplyReader.close();
httpUrlConnInfWebSvc.disconnect();
//System.out.println(infWebSvcReplyString);
return infWebSvcReplyString ;
}
}
Servlet 代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String id = request.getParameter("IdNumber");
MessageConnector connector = new MessageConnector();
String responsemessage = connector.callService(id);
System.out.println(responsemessage);
request.setAttribute("responsemessage", responsemessage);
request.getRequestDispatcher("/NewFile.jsp").forward(request, response);
doGet(request, response);
}
【问题讨论】:
-
让我们看看到目前为止您已经尝试过什么,向我们展示您的代码。喜欢代码的程序员
-
这里,贴出代码!我只是收到了这个请求的回复,因为我已经硬编码了。但我想删除硬编码,需要一种可以调用所有 4 个肥皂请求的方法。