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


相关推荐

  • pandas’_pandas常用方法

    pandas’_pandas常用方法文章目录1.pd.Series.fillna官方案例2.pd.DataFrame.fillna官方案例1.pd.Series.fillnaSeries.fillna(value=None,method=None,axis=None,inplace=False,limit=None,downcast=None)描述按照指定的方法填充NA/NaN值参数value:scalar,dict,Series,orDataFrame标量值或字典对象用于填充缺失值要填充的值,该

    2022年8月12日
  • ios事件-触摸事件2(手势 和 pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()的关系)

    ios事件-触摸事件2(手势 和 pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()的关系)ios事件-触摸事件2(手势和pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()、touchesCancelled()的关系)先看效果图本文中,凡是看到xxx(),即表示xxx是一个函数或者方法!!!事件分为事件传递和事件响应,其中,事件响应又称事件处理。具体代码FindViewViewContr…

    2022年7月25日
  • jQuery添加元素的方法

    jQuery添加元素的方法before() 在被选元素之前插入内容 after() 在被选元素之后插入内容 prepend() 在被选元素的开头插入内容 append() 在被选元素的结尾插入内容

    2022年6月22日
  • springsource sts 初使用

    springsource sts 初使用

    2021年8月15日
  • idea切换到远程分支_git看不到远程分支

    idea切换到远程分支_git看不到远程分支描述最近项目建了分支,在并行开发,需要切换远程分支,在网上找了很多资料都不行。远程分支代码始终更新的是其他分支。解决方案话不多说,直接看图。操作上述之后,开始pull下远程代码,完毕后也可发现分支发生变化说明以上只在2016.1.3上验证。

    2022年9月21日
  • STM32F103使用标准库移植RT-Thread标准版的Finsh

    STM32F103使用标准库移植RT-Thread标准版的Finshrtt标准版中stm32的bsp使用的是HAL库,现在想换成标准库。相比于rttnano的移植来说,和标准版的移植还是有不小的差别的,至少我个人这么认为。nano在移植过程中只需要按照#error提示修改好board.c即可。当然如果需要使用finsh的话,你还需要补充完成finsh_board.c和串口的一些信号,具体参见官方文档。整个流程还是相当清晰的,工作量也不大。移植标准版的另一个原因是我想使用ATClient。刚开始我尝试直接向nano版本中进行添加,但似乎…

    2022年5月15日

发表回复

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

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