本文目錄一覽:
java如何實現發送短信驗證碼功能?
1、創建一個Http的模擬請求工具類,然後寫一個POST方法或者GET方法
/** * 文件說明 * @Description:擴展說明 * @Copyright: XXXX dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util; import java.io.IOException;import java.util.Map; import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.SimpleHttpConnectionManager;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod; /** * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser: feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX * @Version:V6.0 */public class HttpRequestUtil { /** * HttpClient 模擬POST請求 * 方法說明 * @Discription:擴展說明 * @param url * @param params * @return String * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static String postRequest(String url, MapString, String params) { //構造HttpClient的實例 HttpClient httpClient = new HttpClient(); //創建POST方法的實例 PostMethod postMethod = new PostMethod(url); //設置請求頭信息 postMethod.setRequestHeader(“Connection”, “close”); //添加參數 for (Map.EntryString, String entry : params.entrySet()) { postMethod.addParameter(entry.getKey(), entry.getValue()); } //使用系統提供的默認的恢復策略,設置請求重試處理,用的是默認的重試處理:請求三次 httpClient.getParams().setBooleanParameter(“http.protocol.expect-continue”, false); //接收處理結果 String result = null; try { //執行Http Post請求 httpClient.executeMethod(postMethod); //返回處理結果 result = postMethod.getResponseBodyAsString(); } catch (HttpException e) { // 發生致命的異常,可能是協議不對或者返回的內容有問題 System.out.println(“請檢查輸入的URL!”); e.printStackTrace(); } catch (IOException e) { // 發生網絡異常 System.out.println(“發生網絡異常!”); e.printStackTrace(); } finally { //釋放鏈接 postMethod.releaseConnection(); //關閉HttpClient實例 if (httpClient != null) { ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown(); httpClient = null; } } return result; } /** * HttpClient 模擬GET請求 * 方法說明 * @Discription:擴展說明 * @param url * @param params * @return String * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static String getRequest(String url, MapString, String params) { //構造HttpClient實例 HttpClient client = new HttpClient(); //拼接參數 String paramStr = “”; for (String key : params.keySet()) { paramStr = paramStr + “” + key + “=” + params.get(key); } paramStr = paramStr.substring(1); //創建GET方法的實例 GetMethod method = new GetMethod(url + “?” + paramStr); //接收返回結果 String result = null; try { //執行HTTP GET方法請求 client.executeMethod(method); //返回處理結果 result = method.getResponseBodyAsString(); } catch (HttpException e) { // 發生致命的異常,可能是協議不對或者返回的內容有問題 System.out.println(“請檢查輸入的URL!”); e.printStackTrace(); } catch (IOException e) { // 發生網絡異常 System.out.println(“發生網絡異常!”); e.printStackTrace(); } finally { //釋放鏈接 method.releaseConnection(); //關閉HttpClient實例 if (client != null) { ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown(); client = null; } } return result; }}
2、在創建一個類,生成驗證碼,然後傳遞相應的參數(不同的短信平台接口會有不同的參數要求,這個一般短信平台提供的接口文檔中都會有的,直接看文檔然後按要求來即可)
/** * 文件說明 * @Description:擴展說明 * @Copyright: XXXX dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util; import java.net.URLEncoder;import java.util.HashMap;import java.util.Map; /** * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser: feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX * @Version:V6.0 */public class SendMsgUtil { /** * 發送短信消息 * 方法說明 * @Discription:擴展說明 * @param phones * @param content * @return * @return String * @Author: feizi * @Date: 2015年4月17日 下午7:18:08 * @ModifyUser:feizi * @ModifyDate: 2015年4月17日 下午7:18:08 */ @SuppressWarnings(“deprecation”) public static String sendMsg(String phones,String content){ //短信接口URL提交地址 String url = “短信接口URL提交地址”; MapString, String params = new HashMapString, String(); params.put(“zh”, “用戶賬號”); params.put(“mm”, “用戶密碼”); params.put(“dxlbid”, “短信類別編號”); params.put(“extno”, “擴展編號”); //手機號碼,多個號碼使用英文逗號進行分割 params.put(“hm”, phones); //將短信內容進行URLEncoder編碼 params.put(“nr”, URLEncoder.encode(content)); return HttpRequestUtil.getRequest(url, params); } /** * 隨機生成6位隨機驗證碼 * 方法說明 * @Discription:擴展說明 * @return * @return String * @Author: feizi * @Date: 2015年4月17日 下午7:19:02 * @ModifyUser:feizi * @ModifyDate: 2015年4月17日 下午7:19:02 */ public static String createRandomVcode(){ //驗證碼 String vcode = “”; for (int i = 0; i 6; i++) { vcode = vcode + (int)(Math.random() * 9); } return vcode; } /** * 測試 * 方法說明 * @Discription:擴展說明 * @param args * @return void * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static void main(String[] args) {// System.out.println(SendMsgUtil.createRandomVcode());// System.out.println(“ecb=12”.substring(1)); System.out.println(sendMsg(“18123456789,15123456789”, “尊敬的用戶,您的驗證碼為” + SendMsgUtil.createRandomVcode() + “,有效期為60秒,如有疑慮請詳詢XXX-XXX-XXXX【XXX中心】”)); }
然後執行一下,一般的情況下參數傳遞正確,按照接口文檔的規範來操作的話,都會發送成功的,手機都能收到驗證碼的,然後可能會出現的問題就是:發送的短信內容有可能會出現中文亂碼,然後就會發送不成功,按照短信平台的要求進行相應的編碼即可。一般都會是UTF-8編碼。
如何用Java實現短信自動發送功能?
Java實現短信自動發送功能主要是用httpclient實現的,要有發短信的端口。
1、硬件設備是一個3G網卡;
2、軟件方面需要sun提過的java底層通信common包;
3、此外還需要第三方庫SMSLib,這個是開源項目,主要用於實現java發短信的功能;
主要代碼如下:
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(
“”);
post.addRequestHeader(“Content-Type”,
“application/x-www-form-urlencoded;charset=utf-8”);// 在頭文件中設置轉碼
NameValuePair[] data = { new NameValuePair(“sname”, “*****”),
new NameValuePair(“spwd”, “*****”),
new NameValuePair(“scorpid”, “*****”),
new NameValuePair(“sprdid”, “*****”),
new NameValuePair(“sdst”, “*****”),
new NameValuePair(“smsg”, “*****”) };
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println(“statusCode:” + statusCode);
for (Header h : headers) {
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes(“utf-8”));
System.out.println(result);
post.releaseConnection();
Java源程序(.java文件)——java字節碼文件(.class文件)——由解釋執行器(java.exe)將字節碼文件加載到java虛擬機(jvm)——字節碼文件(.class)就會在java虛擬機中執行。
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行複雜的編程 。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點 。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等
java怎麼發送短信
import java.net.URLEncoder;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.io.*;
class http_post
{
public String send_sms(String user_id, String password, String mobile_phone,
String msg, String send_date, String subcode) {
String ret_str = “”;
try {
// Construct data
String data = “user_id=” + user_id + “password=” + password +
“mobile_phone=” + mobile_phone +
“msg=” + URLEncoder.encode(msg, “GBK”) + “send_date=” + send_date +
“subcode=” + subcode;
// Send data
URL url = new URL(“”);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.
getInputStream()));
String line;
while ( (line = rd.readLine()) != null) {
ret_str += line;
}
wr.close();
rd.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
return ret_str;
}
public static void main(String[] args) throws IOException
{
http_post http= new http_post();
String ret=http.send_sms(“4003″,”xxxxxxx”,”13900000000″,”fromjava中國萬歲”,””,”4003″);
System.out.println(ret);
}
}
java怎麼實現群發短信的功能
JAVA實現短信群發的步驟:
1、使用第三方短信平台服務商,接入短信服務;
2、調用短信提交頁面發送請求;
3、服務器向第三方短信平台提交發送請求;
4、短信平台通過運營商將短信下發至用戶的手機上。
以下是秒賽短信平台JAVA短信驗證碼接口代碼示例
package test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
public class Apis {
// 短信發送接口的http地址,請諮詢客服
private static String url = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
// 編碼格式。發送編碼格式統一用UTF-8
private static String ENCODING = “UTF-8”;
public static void main(String[] args) throws IOException, URISyntaxException {
// 賬號
String account = “************************”;
// 密碼
String pswd = “************************”;
// 修改為您要發送的手機號,多個用,分割
String mobile = “13*********”;
// 設置您要發送的內容
String msg = “【秒賽科技】您的驗證碼是:1234”;
// 發短信調用示例
System.out.println(Apis.send(account,pswd, mobile, msg));
}
/**
* 發送短信
*
* @param account
* account
* @param pswd
* pswd
* @param mobile
* 手機號碼
* @param content
* 短信發送內容
*/
public static String send(String account,String pswd, String mobile, String msg) {
NameValuePair[] data = { new NameValuePair(“account”, account),
new NameValuePair(“pswd”, pswd),
new NameValuePair(“mobile”, mobile),
new NameValuePair(“msg”, msg),
new NameValuePair(“needstatus”, “true”),
new NameValuePair(“product”, “”) };
return doPost(url, data);
}
/**
* 基於HttpClient的post函數
* PH
* @param url
* 提交的URL
*
* @param data
* 提交NameValuePair參數
* @return 提交響應
*/
private static String doPost(String url, NameValuePair[] data) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
// method.setRequestHeader(“ContentType”,
// “application/x-www-form-urlencoded;charset=UTF-8”);
method.setRequestBody(data);
// client.getParams()。setContentCharset(“UTF-8”);
client.getParams()。setConnectionManagerTimeout(10000);
try {
client.executeMethod(method);
return method.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
原創文章,作者:SQHUK,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/325099.html