如何在java中调用多个soap服务

如何在java中调用多个soap服务,java,soap,wsdl,Java,Soap,Wsdl,我必须制作一个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.OutputStreamWrite

我必须制作一个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 ;

        }
    }

通常,您不想自己创建soapxml

常用的方法是使用一个接受WSDL的框架,为它生成存根,并在代码中使用对存根的调用

存根和框架处理所有协议内容,包括从java对象生成XML


就是这样一个Java框架。

让我们看看您到目前为止尝试了什么,向我们展示您的代码。程序员喜欢codeHere,发布代码!我刚刚收到这一请求的回复,因为我已经硬编码了。但是我想删除硬编码,并且需要一种方法来调用所有4个soap请求。谢谢你的建议。是否有一个真正的实现示例。请分享一下。
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);
    }