java servlet 手机 app 访问接口 (二) 短信验证
这里有新鲜出炉的 Java 函数式编程, 程序狗速度看过来!
Java 程序设计语言
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言, 是由 Sun Microsystems 公司于 1995 年 5 月推出的 Java 程序设计语言和 Java 平台 (即 JavaEE(j2ee), JavaME(j2me), JavaSE(j2se)) 的总称
这篇文章主要介绍了 java servlet 手机 app 访问接口(二), 短信验证, 具有一定的参考价值, 感兴趣的小伙伴们可以参考一下
今天找了几个短信平台, 其实最想使用的一个是 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 8: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 短信内容这个参数必须写他们规定的, 否则报错, 如果正式购买可以自己定模版内容
来源: http://www.phperz.com/article/18/0205/359129.html