- 浏览: 259688 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
小清新雪仔_:
报错The import org.springframewor ...
Spring Security oAuth学习之Hello World -
hrybird:
http://www.dolit.cn/portfolios/ ...
LightVOD P2P点播系统完全开源 -
xiaoxiao_qiang:
感谢,正在学习oAuth,有机会一起交流
Spring Security oAuth学习之Hello World -
thinkgem:
你好,lightvodcode.zip现在不能下了,能否发我一 ...
LightVOD P2P点播系统完全开源 -
gdw520:
<div style="text-align: ...
如何实现javascript截图 jQuery插件imgAreaSelect使用详解
package com.test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*@author email: yaoxinghuo at 126 dot com
*@version create: Aug 5, 2009 11:17:23 AM
*@version update: Oct 28, 2009 00:11:00 AM
*@description 飞信API(fetionlib) HTTP简单调用举例 另有Restlet的方式可供调用。网页版飞信首页:http://fetionlib.appspot.com/
* 小提示:免费开通飞信:如果您的手机号没有开通飞信,可以到中国移动飞信网站查看开通,或者直接编辑短信KTFX发送到10086开通
* 修改飞信密码:手机编辑新密码(6到16位,不能是中文或全角字符)内容发送到12520050
*
* 2009.11.23更新:已增加简易版飞信界面,可以使用iframe嵌套或者直接使用提供的HTML代码粘贴到您的网页,详情点击http://fetionlib.appspot.com/simple_fetion.html
*
* 直接在浏览器里输入以下地址(您的手机号码和密码请自行更改,密码不要包含/,朋友号码请填写自己的手机号码):
* http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message
* 以上四个用/隔开的加粗的地方,应该分别替换成:您的手机号、密码、对方手机号(可以写自己的手机号发给自己)、短信内容(不超过180字),
* 如果是密码错误,没有开通飞信,对方不是您好友等原因不能发送都是返回提示Message Not Sent,只有成功返回OK
* 如果要发送中文,最好用URLEncode(UTF-8编码,如“你好”Encode后为%E4%BD%A0%E5%A5%BD)或后面举的例子(POST方式,注意调用的URL略有不同)
* 如果您可以收到自己发给自己的短信,恭喜您,测试通过,你可以用您熟悉的语言通过POST或GET调用,调用方式请看下面Java例子,其他语言类似
* 注意,如果您使用Eclipse等IDE,最好文本源代码编码为UTF-8,否则可能得到中文信息产生乱码。
* 如有疑问或对API的接口调用方式有任何更好的建议,欢迎提出宝贵意见
*
* 本飞信API包括的所有功能为:1)获取好友列表 2)发送即时短信 3)发送定时短信 4)删除定时短信 5)邀请添加好友 6)测试能否登录
* 调用方式支持GET和POST,POST方式还支持群发(8个或8个以下好友)和定时群发(定时群发最多30个好友),请看下面例子
*
* 近期发现有人利用本程序给他人发送轰炸短信,给他人造成严重骚扰,同时也大量消耗本站资源,已作如下限制:
* 同一个手机号给同一个好友的发短信API以及其他的API(如:添加好友、获取好友列表等)请求间隔为30秒,30秒内的类似请求将无法完成。
* 注:考虑到实际需要,给自己发送短信(手机号和对方好友号码相同或者群发好友里面包含自己手机号)的API请求将不会有30秒时间间隔的限制!
*
* 重要提示:近期所有appspot的https都惨遭RESET,请先前使用API的用户,把调用地址中https改成http
*
* 本飞信API接口程序由Google强力驱动、免费托管,将长期保留,示例程序用到的json包,请到www.json.org下载jar包,也可到这里下载
*/
public class Test {
private static Log log = LogFactory.getLog(Test.class);
public static void main(String[] args) {
//测试发短信,注意:相同手机号,相同好友的请求的调用间隔要超过30秒(除非好友中包含你自己的手机号),否则不成功(responseCode:406)
boolean b = fetchToSendSMS("13812345678", "12345678", new String[] { "13812345678" }, "TestMessage");
System.out.println("Send Message result:" + b);
//测试取得好友列表
// JSONArray friends = fetchToGetFriends("13812345678", "12345678");
// System.out.println("friends:\r\n"+ (friends == null ? "null" : friends.toString()));
//测试添加好友
// int result = fetchToAddFriend("13812345678", "12345678","13812345678","自己姓名", "对方姓名");
// System.out.println("Add Friend result:"+result);
//测试发送定时短信(注意是太平洋时间,所以2009-10-09 01:00 是北京时间09:00发奥)
// String sid = fetchToSendScheduleMsg("13812345678", "12345678", new String[]{"13912345678"}, "TestScheduleMessage", "2009-10-09 01:00");
// System.out.println("sid:"+sid);
//测试删除定时短信
// boolean b2 = fetchToDeleteScheduleMsg("13812345678", "12345678", new String[]{"aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM")};
// System.out.println("schedule message delete result:"+b2);
//测试能否登录(以测试密码是否正确)
// boolean b3 = fetchToLoginTest("13812345678", "12345678"};
// System.out.println("login test result:"+b3);
}
private static final int TRY_TIMES = 3;
private static final int TIME_OUT = 30000;
/**
*发送短消息 更简单的Get方式(中文请先URLEncode,不支持群发,如要群发用下面POST方式,已更新),直接在浏览器里输入以下地址,手机号码和密码请自行改掉:
* http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message 成功返回OK
* 否则返回Message Not Sent,如果要群发或者您的密码包含/或者需要提交中文消息避免可能的乱码最好请用以下的程序(POST方式)
*@param friends
* 短信接收方的好友们
* 注意参数String[] friends 中的数组可以是好友的手机号,也可以是后面用程序取到的好友的uri,详见后面取得好友列表的说明
* 如fetchToSendSMS("13812345678","password",new String[]{"sip:12345678@fetion.com.cn;p=5065","13912345678","tel:15912345678"},"Test");
* 好友数不能超过8个,如果有需要,请用程序分开来多次调用
* 注意:相同手机号,相同好友的请求的调用间隔要超过30秒,否则不成功(responseCode:406),但接受好友中包含你自己的手机号的请求不受30秒的限制!
*@param message
* 短信内容,字数不能超过180字
*/
public static boolean fetchToSendSMS(String mobile, String password,
String[] friends, String message) {
// 加上UUID的目的是防止这样的情况,在服务器上已经成功发送短信,却在返回结果过程中遇到错误,
// 而导致客户端继续尝试请求,此时让服务器根据UUID分辨出该请求已经发送过,避免再次发送短信。
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
int responseCode = 0;
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&friend=" + convertArrayToJSONString(friends)
+ "&message=" + URLEncoder.encode(message, "utf-8");
out.writeBytes(content);
out.flush();
out.close();
responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode == 202)
return true;
else
return false;
} catch (Exception e) {
log.warn("error fetchToSendSMS, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return false;
}
/**
*取得好友列表 GET方式为:
* http://fetionlib.appspot.com/restlet/fetion/friendsList/13812345678/password
* 成功将返回JSON格式的好友列表,如果您不了解JSON格式,请先网上查阅相关知识,
* 如:[{"nickname":"Jerry","localname":"小张","uri":"sip:123456@fetion.com.cn;p=6012","mobile":"13912345678"}]
* 其中nickname是对方给自己设置的昵称,localname是您给对方设置的名字,mobile是对方公开的手机号,uri是该用户的标识符,可用于发送短信时传递的参数
* 注意nickname、localname、mobile 这三个字段可能为空,如果为空,将不会再JSON中显示!
* 如果因得到好友的信息是乱码而报错,请在IDE中将源码编码改成UTF-8,或者手动修改读取Stream的编码方式为UTF-8
* 不成功返回空白
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*
* 您从JSONArray中取得的uri,如sip:123456@fetion.com.cn;p=6012或可能为tel:13912345678,
* 可直接作为参数传入上面的例子中发送短信, 如果有mobile,也可以传入mobile如13916416465,
* 不过有些时候,对方不公开手机号,便无法获取手机号,只有通过uri来发送短信
*
*/
public static JSONArray fetchToGetFriends(String mobile, String password) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/friendsList");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password;
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
if (responseCode == 202) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
return new JSONArray(sb.toString());
} else {
connection.disconnect();
}
} catch (Exception e) {
log.warn("error fetchToGetFriends, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return null;
}
/**
*邀请好友 GET方式为(中文请先URLEncode):
* http://fetionlib.appspot.com/restlet/fetion/friend/13812345678/password/13912345678/Name/FriendName 返回数字-1或0或1,见下面说明
*
*@param friend
* 被邀请好友的手机号
*@param desc
* 您的姓名(不能超过5个字),对方收到邀请短信时,会显示这个名字,以便让对方知道您是谁
*@param nickname
* 对方的姓名(不能超过10个字),如果对方同意的话,这个名字会作为您的好友名称显示
*
*@return -1错误或者对方手机号不支持(如全球通用户), 0对方已经是您的好友 1成功发送邀请短信,等待对方回复是否同意
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*/
public static int fetchToAddFriend(String mobile, String password,
String friend, String desc, String nickname) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
int responseCode = 0;
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/friend");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&friend=" + friend
+ "&desc=" + URLEncoder.encode(desc, "utf-8")
+ "&nickname=" + URLEncoder.encode(nickname, "utf-8");
out.writeBytes(content);
out.flush();
out.close();
responseCode = connection.getResponseCode();
if (responseCode == 202) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
JSONObject jo = new JSONObject(sb.toString());
return jo.getInt("action");
} else {
connection.disconnect();
return -1;
}
} catch (Exception e) {
log.warn("error fetchToAddFriend, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return -1;
}
/**
*发送定时短信 GET方式为(注意中文请先URLEncode,时间是太平洋时间和北京时间时差8小时,GET方式不支持群发,如要群发用下面POST方式):
* http://fetionlib.appspot.com/restlet/fetion/schedule/13812345678/password/13912345678/message/2009-08-08%2012:18 成功返回sid号码,否则返回空白(空格)
*
*POST方式如下
*
*@param message
* 短信内容,字数不能超过180字
*@param date
* 发送日期格式为yyyy-MM-dd HH:mm,注意日期为时区为0的标准时间,北京时间的时区是8,所以要减去8小时;
* 如计划2009-08-08 20:18分发送,应该填写2009-08-08 12:18;
* 中国移动还规定日期要超出现在时间20分钟但不能超过1年。
*@param friends
* 接受短信的好友们,其中的数组可以是好友的手机号,也可以是用程序取到的好友的uri,注意好友数不能超过30个,如果有需要,请用程序分开来多次调用
* 注意:相同手机号,相同好友的请求的调用间隔要超过30秒,否则不成功(responseCode:406),但接受好友中包含你自己的手机号的请求不受30秒的限制!
*
*@return 一个sid号码,记下来如果后续要撤销短信发送,需要用到这个号码
*/
public static String fetchToSendScheduleMsg(String mobile, String password,
String[] friends, String message, String date) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/schedule");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&friend=" + convertArrayToJSONString(friends)
+ "&schedule=" + date.replace(" ", "%20") + "&message="
+ URLEncoder.encode(message, "utf-8");
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
if (responseCode == 202) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
JSONObject jo = new JSONObject(sb.toString());
return jo.getString("sid");
} else {
connection.disconnect();
return null;
}
} catch (Exception e) {
log.warn("error fetchToSaveSchedule, exception:"
+ e.getMessage() + ". tried " + i + " times");
}
}
return null;
}
/**
*删除定时短信 GET方式为:
* http://fetionlib.appspot.com/restlet/fetion/scheduleDelete/13812345678/password/aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM
* aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM是你发送定时短信返回的sid号码,
* GET方式只支持一次删除一个定时短信,如果要删除多个,请用下面的POST方式,成功返回OK,否则返回Schedule Not Deleted
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*
*@param sid
* 发送定时短信时返回的那些sid号码(不能超过10个sid),多个用数组的形式,程序会转换成JSON提交
*
*/
public static boolean fetchToDeleteScheduleMsg(String mobile,
String password, String[] sids) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/scheduleDelete");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&sids="
+ convertArrayToJSONString(sids);
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode == 202)
return true;
else
return false;
} catch (Exception e) {
log.warn("error fetchToDeleteSchedule, exception:"
+ e.getMessage() + ". tried " + i + " times");
}
}
return false;
}
/**
*测试能否登录成功 GET方式为:
* http://fetionlib.appspot.com/restlet/fetion/loginTest/13812345678/password
* 成功返回OK,不成功返回Login Failed
*
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*/
public static boolean fetchToLoginTest(String mobile, String password) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/loginTest");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password;
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode == 202)
return true;
else
return false;
} catch (Exception e) {
log.warn("error fetchToLoginTest, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return false;
}
//把数组转化成JSONString
private static String convertArrayToJSONString(String[] arr) throws Exception {
JSONArray ja = new JSONArray();
for (String a : arr)
ja.put(a);//ja.add(a);//?
return URLEncoder.encode(ja.toString(), "UTF-8");
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*@author email: yaoxinghuo at 126 dot com
*@version create: Aug 5, 2009 11:17:23 AM
*@version update: Oct 28, 2009 00:11:00 AM
*@description 飞信API(fetionlib) HTTP简单调用举例 另有Restlet的方式可供调用。网页版飞信首页:http://fetionlib.appspot.com/
* 小提示:免费开通飞信:如果您的手机号没有开通飞信,可以到中国移动飞信网站查看开通,或者直接编辑短信KTFX发送到10086开通
* 修改飞信密码:手机编辑新密码(6到16位,不能是中文或全角字符)内容发送到12520050
*
* 2009.11.23更新:已增加简易版飞信界面,可以使用iframe嵌套或者直接使用提供的HTML代码粘贴到您的网页,详情点击http://fetionlib.appspot.com/simple_fetion.html
*
* 直接在浏览器里输入以下地址(您的手机号码和密码请自行更改,密码不要包含/,朋友号码请填写自己的手机号码):
* http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message
* 以上四个用/隔开的加粗的地方,应该分别替换成:您的手机号、密码、对方手机号(可以写自己的手机号发给自己)、短信内容(不超过180字),
* 如果是密码错误,没有开通飞信,对方不是您好友等原因不能发送都是返回提示Message Not Sent,只有成功返回OK
* 如果要发送中文,最好用URLEncode(UTF-8编码,如“你好”Encode后为%E4%BD%A0%E5%A5%BD)或后面举的例子(POST方式,注意调用的URL略有不同)
* 如果您可以收到自己发给自己的短信,恭喜您,测试通过,你可以用您熟悉的语言通过POST或GET调用,调用方式请看下面Java例子,其他语言类似
* 注意,如果您使用Eclipse等IDE,最好文本源代码编码为UTF-8,否则可能得到中文信息产生乱码。
* 如有疑问或对API的接口调用方式有任何更好的建议,欢迎提出宝贵意见
*
* 本飞信API包括的所有功能为:1)获取好友列表 2)发送即时短信 3)发送定时短信 4)删除定时短信 5)邀请添加好友 6)测试能否登录
* 调用方式支持GET和POST,POST方式还支持群发(8个或8个以下好友)和定时群发(定时群发最多30个好友),请看下面例子
*
* 近期发现有人利用本程序给他人发送轰炸短信,给他人造成严重骚扰,同时也大量消耗本站资源,已作如下限制:
* 同一个手机号给同一个好友的发短信API以及其他的API(如:添加好友、获取好友列表等)请求间隔为30秒,30秒内的类似请求将无法完成。
* 注:考虑到实际需要,给自己发送短信(手机号和对方好友号码相同或者群发好友里面包含自己手机号)的API请求将不会有30秒时间间隔的限制!
*
* 重要提示:近期所有appspot的https都惨遭RESET,请先前使用API的用户,把调用地址中https改成http
*
* 本飞信API接口程序由Google强力驱动、免费托管,将长期保留,示例程序用到的json包,请到www.json.org下载jar包,也可到这里下载
*/
public class Test {
private static Log log = LogFactory.getLog(Test.class);
public static void main(String[] args) {
//测试发短信,注意:相同手机号,相同好友的请求的调用间隔要超过30秒(除非好友中包含你自己的手机号),否则不成功(responseCode:406)
boolean b = fetchToSendSMS("13812345678", "12345678", new String[] { "13812345678" }, "TestMessage");
System.out.println("Send Message result:" + b);
//测试取得好友列表
// JSONArray friends = fetchToGetFriends("13812345678", "12345678");
// System.out.println("friends:\r\n"+ (friends == null ? "null" : friends.toString()));
//测试添加好友
// int result = fetchToAddFriend("13812345678", "12345678","13812345678","自己姓名", "对方姓名");
// System.out.println("Add Friend result:"+result);
//测试发送定时短信(注意是太平洋时间,所以2009-10-09 01:00 是北京时间09:00发奥)
// String sid = fetchToSendScheduleMsg("13812345678", "12345678", new String[]{"13912345678"}, "TestScheduleMessage", "2009-10-09 01:00");
// System.out.println("sid:"+sid);
//测试删除定时短信
// boolean b2 = fetchToDeleteScheduleMsg("13812345678", "12345678", new String[]{"aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM")};
// System.out.println("schedule message delete result:"+b2);
//测试能否登录(以测试密码是否正确)
// boolean b3 = fetchToLoginTest("13812345678", "12345678"};
// System.out.println("login test result:"+b3);
}
private static final int TRY_TIMES = 3;
private static final int TIME_OUT = 30000;
/**
*发送短消息 更简单的Get方式(中文请先URLEncode,不支持群发,如要群发用下面POST方式,已更新),直接在浏览器里输入以下地址,手机号码和密码请自行改掉:
* http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message 成功返回OK
* 否则返回Message Not Sent,如果要群发或者您的密码包含/或者需要提交中文消息避免可能的乱码最好请用以下的程序(POST方式)
*@param friends
* 短信接收方的好友们
* 注意参数String[] friends 中的数组可以是好友的手机号,也可以是后面用程序取到的好友的uri,详见后面取得好友列表的说明
* 如fetchToSendSMS("13812345678","password",new String[]{"sip:12345678@fetion.com.cn;p=5065","13912345678","tel:15912345678"},"Test");
* 好友数不能超过8个,如果有需要,请用程序分开来多次调用
* 注意:相同手机号,相同好友的请求的调用间隔要超过30秒,否则不成功(responseCode:406),但接受好友中包含你自己的手机号的请求不受30秒的限制!
*@param message
* 短信内容,字数不能超过180字
*/
public static boolean fetchToSendSMS(String mobile, String password,
String[] friends, String message) {
// 加上UUID的目的是防止这样的情况,在服务器上已经成功发送短信,却在返回结果过程中遇到错误,
// 而导致客户端继续尝试请求,此时让服务器根据UUID分辨出该请求已经发送过,避免再次发送短信。
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
int responseCode = 0;
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&friend=" + convertArrayToJSONString(friends)
+ "&message=" + URLEncoder.encode(message, "utf-8");
out.writeBytes(content);
out.flush();
out.close();
responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode == 202)
return true;
else
return false;
} catch (Exception e) {
log.warn("error fetchToSendSMS, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return false;
}
/**
*取得好友列表 GET方式为:
* http://fetionlib.appspot.com/restlet/fetion/friendsList/13812345678/password
* 成功将返回JSON格式的好友列表,如果您不了解JSON格式,请先网上查阅相关知识,
* 如:[{"nickname":"Jerry","localname":"小张","uri":"sip:123456@fetion.com.cn;p=6012","mobile":"13912345678"}]
* 其中nickname是对方给自己设置的昵称,localname是您给对方设置的名字,mobile是对方公开的手机号,uri是该用户的标识符,可用于发送短信时传递的参数
* 注意nickname、localname、mobile 这三个字段可能为空,如果为空,将不会再JSON中显示!
* 如果因得到好友的信息是乱码而报错,请在IDE中将源码编码改成UTF-8,或者手动修改读取Stream的编码方式为UTF-8
* 不成功返回空白
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*
* 您从JSONArray中取得的uri,如sip:123456@fetion.com.cn;p=6012或可能为tel:13912345678,
* 可直接作为参数传入上面的例子中发送短信, 如果有mobile,也可以传入mobile如13916416465,
* 不过有些时候,对方不公开手机号,便无法获取手机号,只有通过uri来发送短信
*
*/
public static JSONArray fetchToGetFriends(String mobile, String password) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/friendsList");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password;
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
if (responseCode == 202) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
return new JSONArray(sb.toString());
} else {
connection.disconnect();
}
} catch (Exception e) {
log.warn("error fetchToGetFriends, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return null;
}
/**
*邀请好友 GET方式为(中文请先URLEncode):
* http://fetionlib.appspot.com/restlet/fetion/friend/13812345678/password/13912345678/Name/FriendName 返回数字-1或0或1,见下面说明
*
*@param friend
* 被邀请好友的手机号
*@param desc
* 您的姓名(不能超过5个字),对方收到邀请短信时,会显示这个名字,以便让对方知道您是谁
*@param nickname
* 对方的姓名(不能超过10个字),如果对方同意的话,这个名字会作为您的好友名称显示
*
*@return -1错误或者对方手机号不支持(如全球通用户), 0对方已经是您的好友 1成功发送邀请短信,等待对方回复是否同意
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*/
public static int fetchToAddFriend(String mobile, String password,
String friend, String desc, String nickname) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
int responseCode = 0;
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/friend");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&friend=" + friend
+ "&desc=" + URLEncoder.encode(desc, "utf-8")
+ "&nickname=" + URLEncoder.encode(nickname, "utf-8");
out.writeBytes(content);
out.flush();
out.close();
responseCode = connection.getResponseCode();
if (responseCode == 202) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
JSONObject jo = new JSONObject(sb.toString());
return jo.getInt("action");
} else {
connection.disconnect();
return -1;
}
} catch (Exception e) {
log.warn("error fetchToAddFriend, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return -1;
}
/**
*发送定时短信 GET方式为(注意中文请先URLEncode,时间是太平洋时间和北京时间时差8小时,GET方式不支持群发,如要群发用下面POST方式):
* http://fetionlib.appspot.com/restlet/fetion/schedule/13812345678/password/13912345678/message/2009-08-08%2012:18 成功返回sid号码,否则返回空白(空格)
*
*POST方式如下
*
*@param message
* 短信内容,字数不能超过180字
*@param date
* 发送日期格式为yyyy-MM-dd HH:mm,注意日期为时区为0的标准时间,北京时间的时区是8,所以要减去8小时;
* 如计划2009-08-08 20:18分发送,应该填写2009-08-08 12:18;
* 中国移动还规定日期要超出现在时间20分钟但不能超过1年。
*@param friends
* 接受短信的好友们,其中的数组可以是好友的手机号,也可以是用程序取到的好友的uri,注意好友数不能超过30个,如果有需要,请用程序分开来多次调用
* 注意:相同手机号,相同好友的请求的调用间隔要超过30秒,否则不成功(responseCode:406),但接受好友中包含你自己的手机号的请求不受30秒的限制!
*
*@return 一个sid号码,记下来如果后续要撤销短信发送,需要用到这个号码
*/
public static String fetchToSendScheduleMsg(String mobile, String password,
String[] friends, String message, String date) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/schedule");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&friend=" + convertArrayToJSONString(friends)
+ "&schedule=" + date.replace(" ", "%20") + "&message="
+ URLEncoder.encode(message, "utf-8");
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
if (responseCode == 202) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
JSONObject jo = new JSONObject(sb.toString());
return jo.getString("sid");
} else {
connection.disconnect();
return null;
}
} catch (Exception e) {
log.warn("error fetchToSaveSchedule, exception:"
+ e.getMessage() + ". tried " + i + " times");
}
}
return null;
}
/**
*删除定时短信 GET方式为:
* http://fetionlib.appspot.com/restlet/fetion/scheduleDelete/13812345678/password/aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM
* aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM是你发送定时短信返回的sid号码,
* GET方式只支持一次删除一个定时短信,如果要删除多个,请用下面的POST方式,成功返回OK,否则返回Schedule Not Deleted
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*
*@param sid
* 发送定时短信时返回的那些sid号码(不能超过10个sid),多个用数组的形式,程序会转换成JSON提交
*
*/
public static boolean fetchToDeleteScheduleMsg(String mobile,
String password, String[] sids) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/scheduleDelete");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password + "&sids="
+ convertArrayToJSONString(sids);
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode == 202)
return true;
else
return false;
} catch (Exception e) {
log.warn("error fetchToDeleteSchedule, exception:"
+ e.getMessage() + ". tried " + i + " times");
}
}
return false;
}
/**
*测试能否登录成功 GET方式为:
* http://fetionlib.appspot.com/restlet/fetion/loginTest/13812345678/password
* 成功返回OK,不成功返回Login Failed
*
* 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
*/
public static boolean fetchToLoginTest(String mobile, String password) {
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < TRY_TIMES; i++) {
try {
URL postUrl = new URL(
"http://fetionlib.appspot.com/restlet/fetion/loginTest");
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "mobile=" + mobile + "&uuid=" + uuid
+ "&password=" + password;
out.writeBytes(content);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode == 202)
return true;
else
return false;
} catch (Exception e) {
log.warn("error fetchToLoginTest, exception:" + e.getMessage()
+ ". tried " + i + " times");
}
}
return false;
}
//把数组转化成JSONString
private static String convertArrayToJSONString(String[] arr) throws Exception {
JSONArray ja = new JSONArray();
for (String a : arr)
ja.put(a);//ja.add(a);//?
return URLEncoder.encode(ja.toString(), "UTF-8");
}
}
发表评论
-
【工具】ANT的安装和配置(windows)
2011-08-16 13:07 9451、下载:到ANT官方网站http://ant.apache. ... -
使用Axis开发Web Service程序
2011-06-16 08:32 837今天差点又要在公司加 ... -
flowplayer的破解
2011-04-24 13:53 9246这些天作视频项目时用到了开源的flowplayer,再网上下了 ... -
网站开发流程-WEB开发的流程
2011-04-21 08:18 10961. web开发流程概括 1.) ... -
Java调用linux命令ffmpeg视频转码
2011-04-01 17:54 1916Java调用linux命令ffmpeg视频转码 java.i ... -
FFmpeg linux下標准安裝 及 web使用
2011-04-01 17:53 35451,下載LAME(Lame Aint an MP3 Encod ... -
安装和使用ffmpeg转换视频为flv文件(windows和linux)
2011-04-01 17:49 2035安装和使用ffmpeg转换视频为flv文件(windows和l ... -
java调用dll
2011-03-02 17:34 1731一.在程序用jnative调用window的dll 1. ... -
java处理日期时间 相加减
2011-02-28 16:33 925JAVA处理日期时间常用方法: 1.java.util.C ... -
java随机生成十六进制颜色
2010-12-17 09:02 7499/** * 获取十六进制的颜色代码.例如 &qu ... -
java保留小数位问题
2010-12-08 16:07 2099java保留两位小数问题: 补充 (double)(Ma ... -
要和.net进行一次交互HTTPt发过来的XML格式
2010-08-13 15:05 1051今天干活时有个需求,要和.net进行一次交互,想在Struts ... -
LightVOD P2P点播系统完全开源
2010-06-21 09:55 6313http://www.lightvod.com 源码下载及配 ... -
排序复习
2010-03-10 16:33 766package algorithms; /** * @a ... -
The content of element type "web-app" must match解决
2010-01-26 12:46 1834The content of element type &qu ... -
JAVA错误处理大集合
2009-12-23 15:29 16760、 需要标识符 a) 不在函数内 1、 非法表达式开始 b) ...
相关推荐
import java.io.IOException; ... ... ... ...public static void main(String[] args) throws Exception{ final Fetion fetion = new Fetion("phone","password");...System.out.println("excute[" + e.getBody().trim()....
java利用飞信API发送短信 java利用飞信API发送短信
飞信webapi短信发送php开源版, 当然改成其它语言也没什么难度。
飞信feition--免费给手机发短信的软件!
飞信mac版---不容易下到啊,找了好久了,终于有了,赶快行动吧
用JAVA发送短信,利用飞信的API来发送
关于飞信api关于飞信api关于飞信api关于飞信api关于飞信api
飞信发送API网上有很多,但没有多少是我自己满意的。很多网站提供基于Web的API调用方式向用户提供服务,但是作为使用者我心里还是没底。我总是担心自己的密码会被某些人记录,一直想写一个自己用的PHP版本飞信发送...
飞信的API在lib里面fetion-java-api.jar~~ 还说明一点,飞信大家都知道,免费的前提是自己开通飞信并且只能发送给飞信好友,所以别抱怨说还要密码什么的! !!!!!!!!!!!!!!!!!!!! 有童鞋说不能...
飞信API网页接口 可以在自己的网站上接受短信
最近在网上看了一下,有很多利用飞信协议发送短信的程序,有基于Web的、有脚本写的、有C#写的、还有Delphi写的;一些是以Web API提供的,还有一些是以客户端用户界面形式提供的。感觉好像还缺点什么!于是写了一个...
中国移动专用短信工具,发送完全免费中国移动专用短信工具,发送完全免费
FetionLite是一个小巧的飞信客户端,其实现的功能也是单一的,就是可以在不打开飞信客户端的情况下给自己的好友或自己发送短信,通过此接口开发的程序可以做出很多有用而实际的功能,比如通知提醒等。
php版 飞信API接口 利用socket模拟登录wap版飞信,并模拟发送飞信,好处是不会有验证码,也比较稳定。
本软件调用了http://sms.api.bz在线飞信发送短信接口,可以飞信好友发信息,附上源码供大家学习,欢迎大家指出问题,参与讨论,我的大本营:http://student.csdn.net/?433
如何确定你的山寨机是否支持MRP软件 山寨机MRP软件飞信-MSN-QQ详细.docx
Fetion API UTF-8 发布包.rar 大家可以为自己的网页添加一个新的功能,网页飞信!