WebService服务调用

本文介绍了WebService的基本概念,详细阐述了如何进行WebService服务的调用,并提供了调用测试的相关内容,旨在帮助读者掌握WebService在分布式应用中的使用。

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

1. WebService是什么

Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。

2. 调用


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;


/**
 * 调用webService接口
 */
public class WebServiceToJsonUtils {


    /**
     * 调用webServiceh后返回结果
     *
     * @param methodName   ws的接口名称
     * @param params    需要的参数
     * @return
     */
    public static String getXmlString(String methodName, Map<String, String> params) {
        String xml = callXml(methodName, params);
        //处理不用的xml标签
        if (xml.contains("<soap:Body>")) {
            xml = xml.substring(xml.indexOf("<soap:Body>") + 11, xml.indexOf("</soap:Body>"))
                    .replace(" xmlns=\"http://tempuri.org/\"", "");
        }
        if (xml.contains("<" + methodName + "Response><" + methodName + "Result>")) {
            xml = xml.replace("<" + methodName + "Response><" + methodName + "Result>", "");
            xml = xml.replace("</" + methodName + "Response></" + methodName + "Result>", "");
            if (xml.contains("</" + methodName + "Result></" + methodName + "Response>")) {
                xml = xml.replace("</" + methodName + "Result></" + methodName + "Response>", "");
            }
        }
        return xml;
    }


    /**
     * @param methodName 调用名称
     * @param params     参数
     * @return
     */
    private static String callXml(String methodName, Map<String, String> params) {
        String address = params.get("address");//webservice接口地址
        try {
            //地址
            URL url = new URL(address);
            //打开链接
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //拼接好xml
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sb.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">\n");
            sb.append(" <soap:Header/>\n");
            sb.append("<soap:Body>\n");
            sb.append("<tem:" + methodName).append(">\n");
            sb.append(getParams(params, methodName)).append("\n");
            sb.append("</tem:").append(methodName).append(">\n");
            sb.append("</soap:Body>\n");
            sb.append("</soap:Envelope>\n");
            String xmlStr = sb.toString();
            System.out.println("webService调用入参===========>\n" + xmlStr);
            //设置好header信息
            con.setRequestMethod("POST");
            //con.setRequestProperty("","");
            con.setRequestProperty("Host", "192.168.1.210");
            con.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
            con.setRequestProperty("Content-Length", String.valueOf(xmlStr.getBytes().length));
            //post请求需要设置
            con.setDoOutput(true);
            con.setDoInput(true);
            //对请求body 往里写xml 设置请求参数
            OutputStream ops = con.getOutputStream();
            ops.write(xmlStr.getBytes());
            ops.flush();
            ops.close();
            //设置响应回来的信息
            InputStream ips = con.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = ips.read(buf)) != -1) {
                baos.write(buf, 0, length);
                baos.flush();
            }
            byte[] responsData = baos.toByteArray();
            baos.close();
            //处理响应信息
            return new String(responsData, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询信息失败:" + e.getMessage());
        }
    }


    /**
     * 传入的参数
     *
     * @param params     参数
     * @param methodName 方法名
     * @return
     */
    private static String getParams(Map<String, String> params, String methodName) {
        String queue_id = params.get("queue_id");
        String searchCode = params.get("searchCode");
        StringBuffer sb = new StringBuffer();
        sb.append("<tem:sInput>");
        sb.append(" <![CDATA[");
        //区分不同的接口,在这组装请求参数
        if ("Check".equals(methodName)) {
            sb.append("<data>");
            sb.append("<queue_id>");
            sb.append(queue_id);
            sb.append("</queue_id>");
            sb.append("</data>");
        } else if ("GetQueue".equals(methodName)) {
            sb.append("<data>");
            sb.append("<searchCode>");
            sb.append(searchCode);
            sb.append("</searchCode>");
            sb.append("</data>");
        }
        sb.append("]]>");
        sb.append("</tem:sInput>");
        return sb.toString();
    }

3.调用测试

 public static void main(String[] args) {
        Map<String, String> inMap = Maps.newHashMap();
        inMap.put("searchCode", "条件");
        //调用地址
        inMap.put("address", "http://localhost:8080/XXX/services/xxxxxxImpl");
        String   resultStr = WebServiceToJsonUtils.getXmlString("GetQueue", inMap);
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值