【问题标题】:how to call more than one soap service in java如何在java中调用多个soap服务
【发布时间】: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 个肥皂请求的方法。

标签: java soap wsdl


【解决方案1】:

通常,您不想自己创建 SOAP XML。

常见的方法是使用一个接受 WSDL 的框架,并为其生成存根,然后在代码中调用存根。

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

Axis2就是这样一个Java框架。

【讨论】:

  • 感谢您的建议。是否有一个真正的实现示例。请你分享一下。
猜你喜欢
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多