SMS sending example

Today, SMS verification reminders are commonplace. As a small example, record the process of integrating SMS in the company. In fact, these demos are generally provided by SMS service providers. As an example of a common application, remember it.
package com.jq.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;



public class SmsClient {
	protected String smsSvcUrl = "http://****:8860"; //Server URL address
	protected String cust_code = "****";									 //账号
	protected String password = "****"; //password
	protected String sp_code = "****"; //Access code (extension code)
	
	public void sendSms(String mobiles, String content) throws IOException {
		sendSms(mobiles, content, sp_code, 0);
	}

	public void sendSms(String mobiles, String content, long task_id)
			throws IOException {
		sendSms(mobiles, content, sp_code, task_id);
	}

	public void sendSms(String mobiles, String content, String sp_code)
			throws IOException {
		sendSms(mobiles, content, sp_code, 0);
	}

	public void sendSms(String mobiles, String content, String sp_code,
			long task_id) throws IOException {
		String urlencContent = URLEncoder.encode(content,"utf-8");
		//String sign = MD5.getMD5((urlencContent + password).getBytes());
        String sign=MD5.sign(urlencContent, password, "utf-8");
		String postData = "content=" + urlencContent + "&destMobiles="
				+ mobiles + "&sign=" + sign + "&cust_code=" + cust_code
				+ "&sp_code=" + sp_code + "&task_id=" + task_id;
		System.err.println(postData);
		URL myurl = new URL(smsSvcUrl);
		URLConnection urlc = myurl.openConnection();
		urlc.setReadTimeout(1000 * 30);
		urlc.setDoOutput(true);
		urlc.setDoInput(true);
		urlc.setAllowUserInteraction(false);

		DataOutputStream server = new DataOutputStream(urlc.getOutputStream());
		//System.out.println("Send data=" + postData);

		server.write(postData.getBytes("utf-8"));
		server.close();

		BufferedReader in = new BufferedReader(new InputStreamReader(
				urlc.getInputStream(), "utf-8"));
		String resXml = "", s = "";
		while ((s = in.readLine()) != null)
			resXml = resXml + s + "\r\n";
		in.close();
		System.out.println("Receive data=" + URLDecoder.decode(resXml,"utf-8"));
	}
	
	public static void main(String[] args) {
		SmsClient client = new SmsClient();
		try {
			client.sendSms("Phone number", "Hello this is test data");
		} catch (IOException e) {
			e.printStackTrace ();
		}
	}
}

package com.jq.test;

import java.io.UnsupportedEncodingException;
import java.security.SignatureException;
import org.apache.commons.codec.digest.DigestUtils;

/**
* Function: Signature processing core file
* Modified date 2012-08-17
* */

public class MD5 {

    /**
     * Signature characters??
     * @param text signature string
     * @param key key
     * @param input_charset encoding format
     * @return signature result
     */
    public static String sign(String text, String key, String input_charset) {
    	text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }
    
    /**
     * Signature characters?
     * @param text signature string
     * @param sign signature result
     * @param key key
     * @param input_charset encoding format
     * @return signature result
     */
    public static boolean verify(String text, String sign, String key, String input_charset) {
    	text = text + key;
    	String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));
    	if(mysign.equals(sign)) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }

    /**
     * @param content
     * @param charset
     * @return
     * @throws SignatureException
     * @throws UnsupportedEncodingException
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("An error occurred during the MD5 signature process?? The specified encoding set is incorrect, the encoding set you currently specified is: " + charset);
        }
    }

}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326677652&siteId=291194637