Today I found several SMS platforms. In fact, the one I want to use most is sharesdk. Using the http API SMS function on it is not only low, but it can also recharge at least 100RMB. However, the review is too strict, and the corresponding APP must also integrate their SMS function, and it will take more than 20 days to upload and review. I just want to find a SMS platform to test it, so it is forgot. Then I just took a random text message platform on Baidu on www.wasun.cn. I felt that it was not bad for the time being. At least the test account it gave did not receive text messages for more than 5 seconds. I looked at it and it was usually 3 seconds or even faster. Next, I will talk about the method of calling the SMS interface and the problems encountered during use.
1. httprequest method request method
The DOMO he gave is actually encapsulated, which is used to request httpClient. He has used this class in .NET before, and there is also the HttpWebRequest class in .net. I looked at the code in java and its function should be encapsulated into the URLConnection class. Due to time and encapsulation methods, I have not studied in-depth research on the Internet, but it should have the same meaning as HttpWebRequest in .net. The following code is posted, including the code of the httpClient class of DEMO generation.
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.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;public class HttpRequest { /** * Send the GET method request to the specified URL* * @param url * URL to send the request * @param param * Request parameters, the request parameters should be in the form of name1=value1&name2=value2. * @return URL Response result of the remote resource represented by the remote resource*/ public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // Open the connection between the URL URLConnection connection = realUrl.openConnection(); // Set the general request attribute connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // Create an actual connection connection.connect(); // Get all response header fields Map<String, List<String>> map = connection.getHeaderFields(); // traverse all response header fields for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // Define the BufferedReader input stream to read the URL's response in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("Exception occurred when sending a GET request!" + e); e.printStackTrace(); } // Use finally block to close the input stream finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * Send a request to the specified URL * @param url * URL to send the request * @param param * Request parameter, the request parameter should be in the form of name1=value1&name2=value2. * @return Response result of the remote resource represented by */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // Open the connection between URLConnection conn = realUrl.openConnection(); // Set the general request attribute conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // To send a POST request, you must set the following two lines to conn.setDoOutput(true); conn.setDoInput(true); // Get the output stream corresponding to the URLConnection object out = new PrintWriter(conn.getOutputStream()); // Send the request parameter out.print(param); // Buffer out.flush() of the flush output stream; // Define the BufferedReader input stream to read the URL response in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("Exception occurred when sending a POST request!" +e); e.printStackTrace(); } //Use finally blocks to close the output stream and the input stream 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; } }2. Official DEMO httpClient method request code
//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("Your verification code is: 7528. Please do not leak the verification code to others."); NameValuePair[] data = {//Submit SMS new NameValuePair("account", "user name"), new NameValuePair("password", "password"), //Password can be encrypted using plaintext password or 32-bit MD5 //new NameValuePair("password", util.StringUtil.MD5Encode("password")), new NameValuePair("mobile", "mobile number"), 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("SMS submission successful"); } } 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 3. Call the encapsulated httprequest code
String phoneMessageParameter=new String("account=?&password=wxhdcs@456&content=Your verification code is: [Variable]. Please do not leak the verification code to others.&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>");If you use this platform, you should pay attention to it. The parameter name in its official document is wrong, the DEMO is correct, and its interface is written in webserver. It returns not json or XML data, but a standard html page, and then all the contents required are written in the tags in the html. If it is a test content content, the parameter must be written as they specified, otherwise an error will be reported. If you purchase it officially, you can set the template content by yourself.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.