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


相关推荐

  • 北京距离最短的地铁线路_北京地铁几号线最挤

    北京距离最短的地铁线路_北京地铁几号线最挤用Python计算北京地铁的两站间最短换乘路线地铁数据地铁数据用字典表示:{station:{neighbor1:linenumber,neighbor2:linenumber,…},station2:{…},…}现在我们有地铁的站名,下面就是如何将地铁站名转化为上面所需要的标准字典格式。从网上找到的地铁站名为字符串:line1=u”’苹果园古城路八角游乐园八宝山玉泉路五

    2025年7月31日
    9
  • 腾讯云— LAMP 架构个人实践分享

    腾讯云— LAMP 架构个人实践分享LAMP环境通常指Linux环境下,由Apache+MySQL/MariaDB+PHP以及其它相关组件组成的网站服务器架构。目前以LAMP组成的Web应用程序平台广泛被应用,70%以上的访问流量由LAMP提供,所以我们也认同LAMP是最强大的网站解决方案。关于LAMP的环境部署文档随处可搜,腾讯官网环境部署介绍可参阅:手动搭建LAMP环境:https://cloud.tencent.com/document/product/213/38402镜像部署LAMP环境:https://cl

    2022年10月16日
    3
  • stardict 安装真人发声包

    stardict 安装真人发声包

    2021年5月3日
    101
  • 情感词典是什么_中文情感分析词典

    情感词典是什么_中文情感分析词典【实例简介】1.褒义词及其近义词;2.否定词典;3.情感词汇本体;4.清华大学中文褒贬词典;5.台湾大学NTUSD情感词典;6.知网情感词典;7.汉语情感极值表;8.情感词典及其分类。【实例截图】【核心代码】SentimentAnalysisDic`–SentimentAnalysisDic|–知网Hownet情感词典||–主张词语(中文).txt||–主张词语(英文)…

    2022年8月23日
    5
  • keypad.h arduino按键操作显示

    keypad.h arduino按键操作显示#include<Keypad.h>constbyteROWS=4;//矩阵键盘行数constbyteCOLS=4;//矩阵键盘列数//按键定义charhexaKeys[ROWS][COLS]={{‘0′,’1′,’2′,’3’},{‘4′,’5′,’6′,’7’},{‘8′,’9′,’A’,’B’},{‘…

    2022年5月2日
    133
  • js数组截取方式splice()和slice()方法

    js数组截取方式splice()和slice()方法js数组截取方式splice()和slice()方法1.splice()splice()方法可以添加元素、删除元素,也可以截取数组片段。删除元素时,将返回被删除的数组片段,因此可以使用splice()方法截取数组片段//传递一个参数,则该方法仅执行删除操作,参数值指定删除元素的起始下标(包含该下标元素)//splice()方法将删除后面所有元素vara=[1,2,3,4,5];//定义数组varb=a.splice(2);//从第三个元素开始执行删除console

    2022年5月25日
    45

发表回复

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

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