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

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

代码语言:javascript
复制
    @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;
}</code></pre></div></div><p>其中由于我这边内网外网差别 ,开始的那些接口网址,帐号,密码参数我是从yml配置文件里读的 后面调用send方法传电话号码 ,短信信息等内容进去 返回的内容还没判断是否成功可自行完善,我是打印出来response可以看到了。</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">&lt;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&gt;

<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.cn/2220.html原文链接: