我必须创建一个java模拟器来调用java中的多个soap请求。 WSDL中有4个Soap请求。我可以通过在java中硬编码XML来调用一个请求,但我想要一种调用soap服务的动态方法。 没有在互联网上找到任何东西。
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);
}
答案 0 :(得分:0)
通常,您不希望自己创建SOAP XML。
通常的方法是使用一个带有WSDL并为其生成存根的框架,并使用对代码中存根的调用。
存根和框架处理所有协议内容,包括从java对象生成XML。
Axis2就是这样的Java框架。