今天找了幾個短信平台,其實最想使用的一個是sharesdk,使用它上面http api短信功能,不僅價格低,而且最少可以充值100RMB,但是審核過於嚴格,對應APP還必須集成他們的短信功能,而且要上傳審核也得20多天,我也只是想找個短信平台測試下而已,所以它就算了。然後就在百度隨便在好了一個短信平台www.wasun.cn,暫時感覺它還不錯,至少它給的測試帳號接受短信的速度沒超過5秒,我看了下一般是3秒甚至更快。 下面我就說說調用短信接口的方法,以及使用中途遇到的問題。
一、httprequest方式請求方法
他給的DOMO其實已經封裝好方法的,是使用httpClient請求的,以前在.NET中使用過這個類, 而且.net中還直接有HttpWebRequest這個類,我看了下代碼在java中它的功能應該是被封裝到了URLConnection這個類裡面,由於時間,封裝的方法我也是從網上找的沒深入研究,不過應該和.net中的HttpWebRequest是一個含義。下面貼代碼,包括DEMO代的httpClient這個類的代碼也一同貼上。
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方式請求代碼
//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代碼
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短信內容這個參數必須寫他們規定的,否則報錯,如果正式購買可以自己定模版內容。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。