WEBSERVICE 短信接口调用使用xml进行参数传递

WEBSERVICE 短信接口调用使用xml进行参数传递

之前找了好久没找到,最后还是同学帮忙的 

    @Value("${sendMessage.url}")
    private String sendUrl;

    @Value("${sendMessage.userId}")
    private String userId;

    @Value("${sendMessage.pwd}")
    private String pwd;

    @Value("${sendMessage.struid}")
    private String struid;

    @Value("${sendMessage.sendTemplete}")
    private String sendTemplete;


    public  Boolean send(String userId,String pwd,String struid,String sendUrl,String sendPhone, String sendContent,String sendDate) throws IOException {
        Boolean flag = false;
        InputStream in = null;
        HttpURLConnection conn = null;
        try {
            File file = ResourceUtils.getFile("classpath:static\\sendMessage.xml");
            InputStream input = new FileInputStream(file);
            Map<String, String> params = new HashMap<>();
            params.put("Userid", userId);
            params.put("Pwd", pwd);
            params.put("struid", struid);
            params.put("strRecNo", sendPhone);
            params.put("strcontent", sendTemplete.replace("$num",sendContent));
            params.put("strsendDate", sendDate);
            String postData = readSoapFile(input, params);
            StringBuilder response = new StringBuilder();
            // 创建URL对象
            URL url = new URL(sendUrl);
            // 连接WebService
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setFollowRedirects(true);
            conn.setAllowUserInteraction(false);
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(120000);
            conn.setReadTimeout(120000);
            OutputStream out = conn.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
            logger.error(params.toString());
            writer.write(postData);
            writer.close();
            out.close();
            if (conn.getResponseCode() != 200) {
                in = conn.getErrorStream();
            } else {
                in = conn.getInputStream();
                flag = true;
            }
            InputStreamReader iReader = new InputStreamReader(in,"UTF-8");
            BufferedReader bReader = new BufferedReader(iReader);
            // 处理返回结果
            String line;
            while ((line = bReader.readLine()) != null) {
                response.append(line + "\n");
            }
            for(int i = 0;i<response.length();i++ ){
                logger.info(response.toString());
            }

            iReader.close();
            bReader.close();
            in.close();
            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            in.close();
            conn.disconnect();
        }
        return flag;
    }
 
    public    String readSoapFile(InputStream input, Map<String, String> params) throws Exception {
        byte[] b = new byte[1024];
        int len = 0;
        int temp = 0;
        while ((temp = input.read()) != -1) {
            b[len] = (byte) temp;
            len++;
        }
        String soapxml = new String(b);

        return replace(soapxml, params);
    }

   
    public   String replace(String param, Map<String, String> params) throws Exception {
        //拼凑占位符使用正则表达式替换之
        String result = param;
        if (params != null && !params.isEmpty()) {
            //拼凑占位符
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String name = "\\$" + entry.getKey();
                Pattern p = Pattern.compile(name);
                Matcher m = p.matcher(result);
                if (m.find()) {
                    result = m.replaceAll(entry.getValue());
                }
            }
        }
        return result;
    }

其中由于我这边内网外网差别 ,开始的那些接口网址,帐号,密码参数我是从yml配置文件里读的 后面调用send方法传电话号码 ,短信信息等内容进去 返回的内容还没判断是否成功可自行完善,我是打印出来response可以看到了。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="xxxx.xxxx" xmlns:xsd="xxxx.xxx" xmlns:soap="http://xxxxx.xxxx/">
    <soap:Body>
        <CheckAndSMS xmlns="xxxx.xxxx">
            <Userid>$Userid</Userid>
            <Pwd>$Pwd</Pwd>
            <struid>$struid</struid>
            <btype>1</btype>
            <strRecNo>$strRecNo</strRecNo>
            <strcontent>$strcontent</strcontent>
            <strsendDate>$strsendDate</strsendDate>
        </CheckAndSMS>
    </soap:Body>
</soap:Envelope>

xml模版是从对应网站考下来的  访问对应接口的网址里面就有模版,特此记录

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2220.html原文链接:https://javaforall.net

(0)
上一篇 2020年11月9日 上午12:46
下一篇 2020年11月9日 上午12:46


相关推荐

  • 口罩、安全帽识别比赛踩坑记(二) 比赛流程及 SSD / YOLO V3 两版本实现[通俗易懂]

    口罩、安全帽识别比赛踩坑记(二) 比赛流程及 SSD / YOLO V3 两版本实现[通俗易懂]本篇文章主要对比赛流程中的各个环节进行展开说明,并对笔者践行过的代码及更改的地方进行记录。如哪里有侵权请联系笔者进行删除。另外在这里对比赛举办方表示感谢~~其中开源代码会在整理后放在github上,并给出相应的链接,这里先留一个小尾巴~~相关有用的链接如下:口罩、安全帽识别比赛踩坑记(一)经验漫谈及随想比赛官方开发环境指导Dockerfile官方文档OpenVINO官方文档…

    2022年5月12日
    78
  • Reactor 模型

    Reactor 模型传统 IO 模型 PPC 是 ProcessPerCo 的缩写 其含义是指每次有新的连接就新建一个进程去专门处理这个连接的请求 TPCshiThread 的缩写 其含义是每次有新的连接就创建一个线程去处理 缺点 资源浪费 每次连接就创建一个线程 结束后线程就销毁了基于以上缺点 我们可以使用线程池进行线程复用 但是如果某个线程阻塞

    2026年3月17日
    1
  • 设计模式——门面模式「建议收藏」

    设计模式——门面模式「建议收藏」今天我们继续来学习前面没有学完的结构型设计模式中的一种:门面模式。门面模式也是一种不太常用的设计模式。所以,我们今天依旧是了解为主,暂时不去深入的学习。概述门面模式:(FacadeDesignPattern)门面模式也叫外观模式,门面模式为子系统提供一组统一的接口,定义一组高层接口让子系统更易用。门面模式原理和实现也比较简单,应用场景也比较明确,主要在接口设计方面使用。何时使用:解决易用性问题。解决性能问题。解决分布式事务问题。UML类图:角色组成:门面角色:客户端调用这个

    2025年6月6日
    5
  • MySQL多表关联查询优化

    MySQL多表关联查询优化背景最近在对运营报表导出进行优化,总结了一些多表关联查询优化的点记录一下。避免临时表通过Explain分析SQL语句,尽量不要使用到临时表。GROUPBY(Explain具体详解,可以看这篇博客)最容易造成使用临时表,GROUPBY与临时表的关系:  1.如果GROUPBY的列没有索引,产生临时表.  2.如果GROUPBY时,SELECT的列不止GROUP…

    2022年5月30日
    44
  • DataGrip破解激活码_通用破解码

    DataGrip破解激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    294
  • 命令提示符运行Python程序

    命令提示符运行Python程序1.打开DOS窗口快捷键windows+R2.切换目录到Python程序所在的目录我的Python程序存放在D:\python\program中,文件为hi.py

    2022年10月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号