<noscript id="eumyk"></noscript>
<menu id="eumyk"><nav id="eumyk"></nav></menu>
  • <menu id="eumyk"></menu>
  • <dd id="eumyk"></dd>
    <xmp id="eumyk"><nav id="eumyk"></nav>
    <xmp id="eumyk"><nav id="eumyk"></nav>
  • 接口文檔

    秒賽為開發者提供了詳細的API文檔和代碼示例,幫助開發者快速接入短信平臺

    JAVA短信接口_JAVA短信發送代碼示例

    2020-03-10 14:50:51 欄目:代碼示例 查看( )
    本文分享java短信接口代碼,為方便用戶接入網站/app/各種系統,感興趣的小伙伴們可以參考一下。

    package com.util;
     
     
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.HashMap;
    import java.util.Map;
     
    /**
     * author: cjianquan
     * date: 2016/9/29
     */
    public class ZxHttpUtil {
     
        // 表示服務器端的url
     
        private ZxHttpUtil() {
            // TODO Auto-generated constructor stub
        }
     
     
        /*
         * params 填寫的URL的參數 encode 字節編碼
         */
        public static String sendPostMessage(String strUrl,Map<String, String> params,
                                             String encode) {
            System.out.println(strUrl);
            URL url = null;
            try {
                url = new URL(strUrl);
            catch (MalformedURLException e) {
                e.printStackTrace();
            }
     
            StringBuffer stringBuffer = new StringBuffer();
     
            if (params != null && !params.isEmpty()) {
                 System.out.println("ddd");
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    try {
                        stringBuffer
                                .append(entry.getKey())
                                .append("=")
                                .append(URLEncoder.encode(entry.getValue(), encode))
                                .append("&");
     
                    catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                // 刪掉最后一個 & 字符
                stringBuffer.deleteCharAt(stringBuffer.length() - 1);
                System.out.println("-->>" + stringBuffer.toString());
     
                try {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url
                            .openConnection();
                    httpURLConnection.setConnectTimeout(3000);
                    httpURLConnection.setDoInput(true);// 從服務器獲取數據
                    httpURLConnection.setDoOutput(true);// 向服務器寫入數據
     
                    // 獲得上傳信息的字節大小及長度
                    byte[] mydata = stringBuffer.toString().getBytes();
                    // 設置請求體的類型
                    httpURLConnection.setRequestProperty("Content-Type",
                            "application/x-www-form-urlencoded");
                    httpURLConnection.setRequestProperty("Content-Lenth",
                            String.valueOf(mydata.length));
     
                    // 獲得輸出流,向服務器輸出數據
                    OutputStream outputStream = (OutputStream) httpURLConnection
                            .getOutputStream();
                    outputStream.write(mydata);
     
                    // 獲得服務器響應的結果和狀態碼
                    int responseCode = httpURLConnection.getResponseCode();
                    System.out.println(responseCode);
                    if (responseCode == 200) {
     
                        // 獲得輸入流,從服務器端獲得數據
                        InputStream inputStream = (InputStream) httpURLConnection
                                .getInputStream();
                        return (changeInputStream(inputStream, encode));
     
                    }
     
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("eee");
            return "";
        }
     
        /*
         * // 把從輸入流InputStream按指定編碼格式encode變成字符串String
         */
        public static String changeInputStream(InputStream inputStream,
                                               String encode) {
     
            // ByteArrayOutputStream 一般叫做內存流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            int len = 0;
            String result = "";
            if (inputStream != null) {
     
                try {
                    while ((len = inputStream.read(data)) != -1) {
                        byteArrayOutputStream.write(data, 0, len);
     
                    }
                    result = new String(byteArrayOutputStream.toByteArray(), encode);
     
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
     
            }
     
            return result;
        }
    }
     
     
     
     
     
     
    package com.util;
     
    import java.security.MessageDigest;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    import com.google.gson.JsonObject;
     
    public class Utility {
      
        public static void jhson() {
             String jsonString ="{"name":"zhangsan","password":"zhangsan123","email":"10371443@qq.com"}";  
             JsonObject json =  new JsonObject();         
        }
        public static String getNowDateStr() {
               Date currentTime = new Date();
               SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
               String dateString = formatter.format(currentTime);
               return dateString;
            }
        
        public static String getMD5(String str) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] bytes = md.digest(str.getBytes("utf-8"));
                System.out.println(toHex(bytes).toLowerCase());
                return toHex(bytes).toLowerCase();
            }
            catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
     
        private static String toHex(byte[] bytes) {
     
            final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
            StringBuilder ret = new StringBuilder(bytes.length * 2);
            for (int i=0; i<bytes.length; i++) {
                ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
                ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
            }
            return ret.toString();
        }
    }
     
     
     
     
     
     
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
         
    <%@page import="java.util.HashMap"%> 
    <%@page import="java.util.Map"%> 
    <%@ page import="com.google.gson.*" %>
    <%@ page import="com.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>SendSms</title>
    </head>
    <body>
    <%
    // 您的短信賬號
    String Account = "您的短信賬號";
    // 您的短信賬號密碼
    String Password = "您的短信賬號密碼";
    // 是否需要狀態報告
    String NeedStatus = "true";
    String message = "短信內容";//短信內容
    String mobile = "xxxxxxxxxxx";//要發送的手機號,多個手機號用,隔開
    String ts =  Utility.getNowDateStr();//時間戳
     
     
    Password = Utility.getMD5(Account + Password + ts);// Md5簽名(賬號+密碼+時間戳)
    HashMap params = new HashMap(); 
    //請求參數 
    params.put("account",Account);
    params.put("pswd",Password);
    params.put("mobile",mobile);
    params.put("msg",message);
    params.put("ts",ts);
    params.put("needstatus",NeedStatus);
    String rep = ZxHttpUtil.sendPostMessage("http://139.196.108.241:8080/Api/HttpSendSMYzm.ashx", params, "UTF-8");
    out.print(rep);
     JsonParser parser = new JsonParser();
     
    JsonObject json = (JsonObject)parser.parse(rep);           
           
    out.print("<br> result_msg:" + json.get("result_msg"));
    %>
    </body>
    </html>
    微信掃一掃

    認證、簽名模板審核結果通知

    賬號短信余額不足提醒

    平臺促銷活動及最新資訊

    鄭重申明:①任何個人和或機構在未經過本人同意的情況下,不得擅自轉載或大段引用到網站或者第三方平臺使用,對于擅自盜用文章將通過法律訴訟途徑等一切手段來解決?、诓糠炙夭膩碓从诨ヂ摼W,如有侵權,請聯系作者刪除!