Java两种对接WebService接口的方法

本文档展示了两种调用WebService接口的方式:一是使用HTTP连接直接发送SOAP请求,二是通过Apache Axis库进行调用。代码示例中包含了设置参数、发送POST请求以及解析返回结果的过程,适用于Java开发人员在集成WebService接口时参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 方法一:通过HTTP访问接口,根据webservice的wsdl说明来设置参数:

 

    public static String sendSoapPost(String url, String xml,
                                      String contentType, String soapAction) {
        HttpURLConnection httpConn = null;
        OutputStream out = null;
        String result = "";
        try {
            httpConn = (HttpURLConnection) new URL(url).openConnection();
            httpConn.setRequestProperty("Content-Type", contentType);
            if (null != soapAction) {
                httpConn.setRequestProperty("SOAPAction", soapAction);
            }
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.connect();
            // 获取输出流对象
            out = httpConn.getOutputStream();
            // 将要提交服务器的SOAP请求字符流写入输出流
            httpConn.getOutputStream().write(xml.getBytes());
            out.flush();
            out.close();
            int code = httpConn.getResponseCode();
            StringBuffer sb1 = new StringBuffer();
            if (code == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader( new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
                String inputLine;
                while (null != (inputLine = reader.readLine())) {
                    Pattern pattern = Pattern.compile("<GetElecReceiptListResult>(.*)</GetElecReceiptListResult>");
                    Matcher matcher = pattern.matcher(inputLine);
                    while (matcher.find()) {
                        result = matcher.group(1);
                    }
                }
                if (null != reader) {
                    reader.close();
                }
            } else {
                InputStream errorStream = httpConn.getErrorStream();
                BufferedReader reader = new BufferedReader( new InputStreamReader(errorStream,"UTF-8"));
                String inputLine;
                // 解析获取结果集
                while (null != (inputLine = reader.readLine())) {
                    Pattern pattern = Pattern.compile("<GetElecReceiptListResult>(.*)</GetElecReceiptListResult>");
                    Matcher matcher = pattern.matcher(inputLine);
                    while (matcher.find()) {
                        result = matcher.group(1);
                    }
                }
                if (null != reader) {
                    reader.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

上述方法调用

   public static void main(String[] args) {
        JSONObject json = new JSONObject();
        json.put("startDate", "20211207");
        json.put("endDate", "20211207");
        String param = json.toJSONString();
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Body>\n" +
                "    <GetElecReceiptList xmlns=\"http://tempuri.org/\">\n" +
                "      <filterStr>" + param + "</filterStr>\n" +
                "    </GetElecReceiptList>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";
        // server域名地址,为了统一规范,一般都是这个域名
        String soapaction = "http://tempuri.org/";
        // 方法名
        try {
            // webService链接地址
            String url = "webService链接地址";

            String contentType = "application/soap+xml; charset=utf-8";
            String resultXml = sendSoapPost(url, xml, contentType,
                    soapaction);
            System.out.println(resultXml);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

方法二:通过axis调用,需要引入相应jar包

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>axis</groupId>
    <artifactId>axis-jaxrpc</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>commons-discovery</groupId>
    <artifactId>commons-discovery</artifactId>
    <version>0.5</version>
</dependency>
<dependency>
    <groupId>wsdl4j</groupId>
    <artifactId>wsdl4j</artifactId>
    <version>1.6.3</version>
</dependency>
    public static void main(String[] args) {
        // server域名地址,为了统一规范,一般都是这个域名
        String soapaction = "http://tempuri.org/";
        // 方法名
        String methodName = "方法名";
        try {
            // webService链接地址
            String url = "webService链接地址";
            // 用户提供测试的两个参数
            JSONObject json = new JSONObject();
            json.put("startDate","20211206");
            json.put("endDate","20211206");
            String param = json.toJSONString();
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            // 设置要调用哪个方法
            call.setOperationName(new QName(soapaction,methodName));
            // 设置要传递的参数名
            call.addParameter(new QName(soapaction,"filterStr"), XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            // 提供标准类型
            call.setReturnType(XMLType.SOAP_STRING);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapaction + methodName);
            // 调用方法并传递参数
            String invoke = (String)call.invoke(new Object[]{param});
            System.out.println(invoke);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值