今天找了幾個短信平臺,其實最想使用的一個是sharesdk,使用它上面http api短信功能,不僅價格低,而且最少可以充值100RMB,但是審核過于嚴格,對應APP還必須集成他們的短信功能,而且要上傳審核也得20多天,我也只是想找個短信平臺測試下而已,所以它就算了。然后就在百度隨便在好了一個短信平臺www.wasun.cn,暫時感覺它還不錯,至少它給的測試帳號接受短信的速度沒超過5秒,我看了下一般是3秒甚至更快。 下面我就說說調用短信接口的方法,以及使用中途遇到的問題。
一、httprequest方式請求方法
他給的DOMO其實已經封裝好方法的,是使用httpClient請求的,以前在.NET中使用過這個類, 而且.net中還直接有HttpWebRequest這個類,我看了下代碼在java中它的功能應該是被封裝到了URLConnection這個類里面,由于時間,封裝的方法我也是從網上找的沒深入研究,不過應該和.net中的HttpWebRequest是一個含義。下面貼代碼,包括DEMO代的httpClient這個類的代碼也一同貼上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
package Helper; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class HttpRequest { /** * 向指定URL發送GET方法的請求 * * @param url * 發送請求的URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠程資源的響應結果 */ public static String sendGet(String url, String param) { String result = "" ; BufferedReader in = null ; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性 connection.setRequestProperty( "accept" , "*/*" ); connection.setRequestProperty( "connection" , "Keep-Alive" ); connection.setRequestProperty( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" ); // 建立實際的連接 connection.connect(); // 獲取所有響應頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷所有的響應頭字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null ) { result += line; } } catch (Exception e) { System.out.println( "發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入流 finally { try { if (in != null ) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所代表遠程資源的響應結果 */ public static String sendPost(String url, String param) { PrintWriter out = null ; BufferedReader in = null ; String result = "" ; try { URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性 conn.setRequestProperty( "accept" , "*/*" ); conn.setRequestProperty( "connection" , "Keep-Alive" ); conn.setRequestProperty( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" ); // 發送POST請求必須設置如下兩行 conn.setDoOutput( true ); conn.setDoInput( true ); // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null ) { result += line; } } catch (Exception e) { System.out.println( "發送 POST 請求出現異常!" +e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally { try { if (out!= null ){ out.close(); } if (in!= null ){ in.close(); } } catch (IOException ex){ ex.printStackTrace(); } } try { result= new String(result.getBytes( "ISO8859-1" ), "UTF-8" ); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } } |
二、官方DEMO httpClient方式請求代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
//import java.io.FileInputStream; //import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class sendsms { private static String Url = "http://121.199.?.178/webservice/sms.php?method=Submit" ; public static void main(String [] args) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(Url); //client.getParams().setContentCharset("GBK"); client.getParams().setContentCharset( "UTF-8" ); method.setRequestHeader( "ContentType" , "application/x-www-form-urlencoded;charset=UTF-8" ); String content = new String( "您的驗證碼是:7528。請不要把驗證碼泄露給其他人。" ); NameValuePair[] data = { //提交短信 new NameValuePair( "account" , "用戶名" ), new NameValuePair( "password" , "密碼" ), //密碼可以使用明文密碼或使用32位MD5加密 //new NameValuePair("password", util.StringUtil.MD5Encode("密碼")), new NameValuePair( "mobile" , "手機號碼" ), new NameValuePair( "content" , content), }; method.setRequestBody(data); try { client.executeMethod(method); String SubmitResult =method.getResponseBodyAsString(); //System.out.println(SubmitResult); Document doc = DocumentHelper.parseText(SubmitResult); Element root = doc.getRootElement(); String code = root.elementText( "code" ); String msg = root.elementText( "msg" ); String smsid = root.elementText( "smsid" ); System.out.println(code); System.out.println(msg); System.out.println(smsid); if (code == "2" ){ System.out.println( "短信提交成功" ); } } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 0 |
三、調用封裝的httprequest代碼
1
2
3
|
String phoneMessageParameter= new String( "account=?&password=wxhdcs@456&content=您的校驗碼是:【變量】。請不要把校驗碼泄露給其他人。&mobile=?&stime=2012-08-01%208:20:23&sign=?&type=pt&extno=" ); String returnResult=HttpRequest.sendPost( "http://121.?.16.178/webservice/sms.php?method=Submit" , phoneMessageParameter); out.println( "<script> alert(" +returnResult+ ");</script>" ); |
如果使用這個平臺要注意下,就是他官方的文檔中的參數名是錯的,發的DEMO中才是正確的,還有他的接口是用webserver寫的,返回的不是json或則XML數據,而是一個標準的html頁面,然后需要的內容都寫在html中的標簽中, 如果是測試content短信內容這個參數必須寫他們規定的,否則報錯,如果正式購買可以自己定模版內容。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。