您的位置:首页 > 编程语言 > Java开发

java servlet手机app访问接口(二)短信验证

2016-12-20 15:26 627 查看

今天找了几个短信平台,其实最想使用的一个是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();
}
}

三、调用封装的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短信内容这个参数必须写他们规定的,否则报错,如果正式购买可以自己定模版内容。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐