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模版是從對應網站考下來的  訪問對應接口的網址裏面就有模版,特此記錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章