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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java 枚举(enum) 详解7种常见的用法

    Java 枚举(enum) 详解7种常见的用法JDK1.5引入了新的类型——枚举。在Java中它虽然算个“小”功能,却给我的开发带来了“大”方便。大师兄我【大师兄】又加上自己的理解,来帮助各位理解一下。用法一:常量在JDK1.5之前,我们定义常量都是:publicstaticfinal….。现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。Java代码publ…

    2022年6月3日
    28
  • jQuery实现方式不一样的跳转到底部

    jQuery跳转到页面底部效果在线体验:http://hovertree.com/texiao/jquery/9.htm以下是完整HTML代码:跳到底部jquery效果-何问起跳到底部效果

    2021年12月21日
    50
  • 最全中文停用词表

    最全中文停用词表本文将github上包括哈工大停用词、四川大学机器智能实验室停用词、百度停用词、中文停用https://github.com/goto456/stopwords以及最全中文停用词表(1893)https://blog.csdn.net/shijiebei2009/article/details/39696571进行整合得到新的中文停用词表,总共2462个为止纵然all例如[④e…

    2022年6月15日
    32
  • linux编辑文件命令vim怎么退出_vim退出命令

    linux编辑文件命令vim怎么退出_vim退出命令一、进入文件vim/etc/profile二、编辑文件按i进行编辑三、保存与退出1.首先按esc键返回命令编辑模式,刚才的Insert会消失2.按英文状态的:3.此时进行:q!不保存文件,强制退出vi命令:w保存文件,不退出vi命令:wq保存文件,退出vi命令4.输入以上命令按enter进行…

    2022年8月24日
    6
  • centos7网络设置ipv4_centos7连接wifi详细步骤

    centos7网络设置ipv4_centos7连接wifi详细步骤系统版本centos7.71、ip配置(配置后局域网内可互ping)同网段内设置,不用网关即可通信1、临时设置#设置接口ens33的地址为192.168.59.27ipaddradd192.168.56.27/24devens33#查看接口ens33地址ipaddrshowens332、永久设置进入目录,/etc/sysconfig/network-scripts,修改文件ifcfg-ens33,修改或添加下列几项BOOTPROTO=”none”#

    2022年5月3日
    190

发表回复

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

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