Java调用飞信API

举报
黄啊码 发表于 2022/06/28 22:54:57 2022/06/28
【摘要】 //由于某些原因,现在只有http://w.ibtf.net/f.php?phone=xxxxxx&pwd=xxx&to=xxxx&msg=xxxx&type=x//package com.test等破解方式才能发送短信,但发送短信条数有限,所以网友们//在下边记得把网址和参数改为以上这种方式,当然如果有...

  
  1. //由于某些原因,现在只有http://w.ibtf.net/f.php?phone=xxxxxx&pwd=xxx&to=xxxx&msg=xxxx&type=x
  2. //package com.test等破解方式才能发送短信,但发送短信条数有限,所以网友们
  3. //在下边记得把网址和参数改为以上这种方式,当然如果有能力的话也可以到淘宝购买飞信API接口,
  4. //相关请看<a target=_blank href="http://www.wuchuimeng.com/30.html">http://www.wuchuimeng.com/30.html</a>
  5. import java.io.BufferedReader;
  6. import java.io.DataOutputStream;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.net.URLEncoder;
  11. import java.util.UUID;
  12. import org.apache.commons.logging.Log;
  13. import org.apache.commons.logging.LogFactory;
  14. import org.json.JSONArray;
  15. import org.json.JSONObject;
  16. public class Test {
  17. private static Log log = LogFactory.getLog(Test.class);
  18. public static void main(String[] args) {
  19. //测试发短信,注意:相同手机号,相同好友的请求的调用间隔要超过30秒(除非好友中包含你自己的手机号),否则不成功
  20. boolean b = fetchToSendSMS("138XXXXXXXX", "12345678", new String[] { "138XXXXXXXX" }, "TestMessage");
  21. System.out.println("Send Message result:" + b);
  22. //测试取得好友列表
  23. // JSONArray friends = fetchToGetFriends("138XXXXXXXX", "12345678");
  24. // System.out.println("friends:\r\n"+ (friends == null ? "null" : friends.toString()));
  25. //测试添加好友
  26. // int result = fetchToAddFriend("138XXXXXXXX", "12345678","138XXXXXXXX","自己姓名", "对方姓名");
  27. // System.out.println("Add Friend result:"+result);
  28. //测试发送定时短信(注意是太平洋时间,所以2009-10-09 01:00 是北京时间09:00)
  29. // String sid = fetchToSendScheduleMsg("138XXXXXXXX", "12345678", new String[]{"138XXXXXXXX"}, "TestScheduleMessage", "2009-10-09 01:00");
  30. // System.out.println("sid:"+sid);
  31. //测试删除定时短信
  32. // boolean b2 = fetchToDeleteScheduleMsg("138XXXXXXXX", "12345678", new String[]{"aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM")};
  33. // System.out.println("schedule message delete result:"+b2);
  34. //测试能否登录(以测试密码是否正确)
  35. // boolean b3 = fetchToLoginTest("138XXXXXXXX", "12345678"};
  36. // System.out.println("login test result:"+b3);
  37. //测试取得好友回复消息
  38. // JSONArray messages = fetchToCheckMessages("138XXXXXXXX", "12345678");
  39. // System.out.println("messages:\r\n"+ (messages == null ? "null" : messages.toString()));
  40. }
  41. private static final int TRY_TIMES = 3;
  42. private static final int TIME_OUT = 30000;
  43. private static final String BASE_URL = "http://fetionlib.appspot.com"; //BASE_URL 目前用http://fetion.xinghuo.org.ru 比较稳定
  44. / * 具体responseCode的代码含义:202-成功 400-参数格式错误 401-密码错误 404-可能不是好友等原因无法发送成功 406- *太频繁 408-超时 500-服务器错误 503-系统维护
  45. *@param message
  46. * 短信内容,字数不能超过180
  47. */
  48. public static boolean fetchToSendSMS(String mobile, String password,
  49. String[] friends, String message) {
  50. // 加上UUID的目的是防止这样的情况,在服务器上已经成功发送短信,却在返回结果过程中遇到错误,
  51. // 而导致客户端继续尝试请求,此时让服务器根据UUID分辨出该请求已经发送过,避免再次发送短信。
  52. String uuid = UUID.randomUUID().toString();
  53. for (int i = 0; i < TRY_TIMES; i++) {
  54. int responseCode = 0;
  55. try {
  56. URL postUrl = new URL(BASE_URL + "/restlet/fetion");
  57. HttpURLConnection connection = (HttpURLConnection) postUrl
  58. .openConnection();
  59. connection.setConnectTimeout(TIME_OUT);
  60. connection.setReadTimeout(TIME_OUT);
  61. connection.setDoOutput(true);
  62. connection.setRequestMethod("POST");
  63. connection.setUseCaches(false);
  64. connection.setInstanceFollowRedirects(true);
  65. connection.setRequestProperty("Content-Type",
  66. "application/x-www-form-urlencoded");
  67. connection.connect();
  68. DataOutputStream out = new DataOutputStream(connection
  69. .getOutputStream());
  70. String content = "mobile=" + mobile + "&uuid=" + uuid
  71. + "&password=" + password + "&friend=" + convertArrayToJSONString(friends)
  72. + "&message=" + URLEncoder.encode(message, "utf-8");
  73. out.writeBytes(content);
  74. out.flush();
  75. out.close();
  76. responseCode = connection.getResponseCode();
  77. connection.disconnect();
  78. if (responseCode == 202)
  79. return true;
  80. else
  81. return false;
  82. } catch (Exception e) {
  83. log.warn("error fetchToSendSMS, exception:" + e.getMessage()
  84. + ". tried " + i + " times");
  85. }
  86. }
  87. return false;
  88. }
  89. /**
  90. *取得好友列表
  91. */
  92. public static JSONArray fetchToGetFriends(String mobile, String password) {
  93. String uuid = UUID.randomUUID().toString();
  94. for (int i = 0; i < TRY_TIMES; i++) {
  95. try {
  96. URL postUrl = new URL(BASE_URL + "/restlet/fetion/friendsList");
  97. HttpURLConnection connection = (HttpURLConnection) postUrl
  98. .openConnection();
  99. connection.setConnectTimeout(TIME_OUT);
  100. connection.setReadTimeout(TIME_OUT);
  101. connection.setDoOutput(true);
  102. connection.setRequestMethod("POST");
  103. connection.setUseCaches(false);
  104. connection.setInstanceFollowRedirects(true);
  105. connection.setRequestProperty("Content-Type",
  106. "application/x-www-form-urlencoded");
  107. connection.connect();
  108. DataOutputStream out = new DataOutputStream(connection
  109. .getOutputStream());
  110. String content = "mobile=" + mobile + "&uuid=" + uuid
  111. + "&password=" + password;
  112. out.writeBytes(content);
  113. out.flush();
  114. out.close();
  115. int responseCode = connection.getResponseCode();
  116. if (responseCode == 202) {
  117. BufferedReader reader = new BufferedReader(
  118. new InputStreamReader(connection.getInputStream(), "UTF-8")); //读取结果
  119. StringBuffer sb = new StringBuffer();
  120. String line;
  121. while ((line = reader.readLine()) != null) {
  122. sb.append(line);
  123. }
  124. reader.close();
  125. connection.disconnect();
  126. return new JSONArray(sb.toString());
  127. } else {
  128. connection.disconnect();
  129. }
  130. } catch (Exception e) {
  131. log.warn("error fetchToGetFriends, exception:" + e.getMessage()
  132. + ". tried " + i + " times");
  133. }
  134. }
  135. return null;
  136. }
  137. /**
  138. *邀请好友
  139. */
  140. public static int fetchToAddFriend(String mobile, String password,
  141. String friend, String desc, String nickname) {
  142. String uuid = UUID.randomUUID().toString();
  143. for (int i = 0; i < TRY_TIMES; i++) {
  144. int responseCode = 0;
  145. try {
  146. URL postUrl = new URL(BASE_URL + "/restlet/fetion/friend");
  147. HttpURLConnection connection = (HttpURLConnection) postUrl
  148. .openConnection();
  149. connection.setConnectTimeout(TIME_OUT);
  150. connection.setReadTimeout(TIME_OUT);
  151. connection.setDoOutput(true);
  152. connection.setRequestMethod("POST");
  153. connection.setUseCaches(false);
  154. connection.setInstanceFollowRedirects(true);
  155. connection.setRequestProperty("Content-Type",
  156. "application/x-www-form-urlencoded");
  157. connection.connect();
  158. DataOutputStream out = new DataOutputStream(connection
  159. .getOutputStream());
  160. String content = "mobile=" + mobile + "&uuid=" + uuid
  161. + "&password=" + password + "&friend=" + friend
  162. + "&desc=" + URLEncoder.encode(desc, "utf-8")
  163. + "&nickname=" + URLEncoder.encode(nickname, "utf-8");
  164. out.writeBytes(content);
  165. out.flush();
  166. out.close();
  167. responseCode = connection.getResponseCode();
  168. if (responseCode == 202) {
  169. BufferedReader reader = new BufferedReader(
  170. new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
  171. StringBuffer sb = new StringBuffer();
  172. String line;
  173. while ((line = reader.readLine()) != null) {
  174. sb.append(line);
  175. }
  176. reader.close();
  177. connection.disconnect();
  178. JSONObject jo = new JSONObject(sb.toString());
  179. return jo.getInt("action");
  180. } else {
  181. connection.disconnect();
  182. return -1;
  183. }
  184. } catch (Exception e) {
  185. log.warn("error fetchToAddFriend, exception:" + e.getMessage()
  186. + ". tried " + i + " times");
  187. }
  188. }
  189. return -1;
  190. }
  191. /**
  192. *发送定时短信
  193. */
  194. public static String fetchToSendScheduleMsg(String mobile, String password,
  195. String[] friends, String message, String date) {
  196. String uuid = UUID.randomUUID().toString();
  197. for (int i = 0; i < TRY_TIMES; i++) {
  198. try {
  199. URL postUrl = new URL(BASE_URL + "/restlet/fetion/schedule");
  200. HttpURLConnection connection = (HttpURLConnection) postUrl
  201. .openConnection();
  202. connection.setConnectTimeout(TIME_OUT);
  203. connection.setReadTimeout(TIME_OUT);
  204. connection.setDoOutput(true);
  205. connection.setRequestMethod("POST");
  206. connection.setUseCaches(false);
  207. connection.setInstanceFollowRedirects(true);
  208. connection.setRequestProperty("Content-Type",
  209. "application/x-www-form-urlencoded");
  210. connection.connect();
  211. DataOutputStream out = new DataOutputStream(connection
  212. .getOutputStream());
  213. String content = "mobile=" + mobile + "&uuid=" + uuid
  214. + "&password=" + password + "&friend=" + convertArrayToJSONString(friends)
  215. + "&schedule=" + date.replace(" ", "%20") + "&message="
  216. + URLEncoder.encode(message, "utf-8");
  217. out.writeBytes(content);
  218. out.flush();
  219. out.close();
  220. int responseCode = connection.getResponseCode();
  221. if (responseCode == 202) {
  222. BufferedReader reader = new BufferedReader(
  223. new InputStreamReader(connection.getInputStream(), "UTF-8")); // 读取结果
  224. StringBuffer sb = new StringBuffer();
  225. String line;
  226. while ((line = reader.readLine()) != null) {
  227. sb.append(line);
  228. }
  229. reader.close();
  230. connection.disconnect();
  231. JSONObject jo = new JSONObject(sb.toString());
  232. return jo.getString("sid");
  233. } else {
  234. connection.disconnect();
  235. return null;
  236. }
  237. } catch (Exception e) {
  238. log.warn("error fetchToSaveSchedule, exception:"
  239. + e.getMessage() + ". tried " + i + " times");
  240. }
  241. }
  242. return null;
  243. }
  244. /**
  245. *删除定时短信
  246. * 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
  247. *
  248. *@param sid
  249. * 发送定时短信时返回的那些sid号码(不能超过10个sid),多个用数组的形式,程序会转换成JSON提交
  250. *
  251. */
  252. public static boolean fetchToDeleteScheduleMsg(String mobile,
  253. String password, String[] sids) {
  254. String uuid = UUID.randomUUID().toString();
  255. for (int i = 0; i < TRY_TIMES; i++) {
  256. try {
  257. URL postUrl = new URL(BASE_URL + "/restlet/fetion/scheduleDelete");
  258. HttpURLConnection connection = (HttpURLConnection) postUrl
  259. .openConnection();
  260. connection.setConnectTimeout(TIME_OUT);
  261. connection.setReadTimeout(TIME_OUT);
  262. connection.setDoOutput(true);
  263. connection.setRequestMethod("POST");
  264. connection.setUseCaches(false);
  265. connection.setInstanceFollowRedirects(true);
  266. connection.setRequestProperty("Content-Type",
  267. "application/x-www-form-urlencoded");
  268. connection.connect();
  269. DataOutputStream out = new DataOutputStream(connection
  270. .getOutputStream());
  271. String content = "mobile=" + mobile + "&uuid=" + uuid
  272. + "&password=" + password + "&sids="
  273. + convertArrayToJSONString(sids);
  274. out.writeBytes(content);
  275. out.flush();
  276. out.close();
  277. int responseCode = connection.getResponseCode();
  278. connection.disconnect();
  279. if (responseCode == 202)
  280. return true;
  281. else
  282. return false;
  283. } catch (Exception e) {
  284. log.warn("error fetchToDeleteSchedule, exception:"
  285. + e.getMessage() + ". tried " + i + " times");
  286. }
  287. }
  288. return false;
  289. }
  290. /**
  291. *测试能否登录成功
  292. * 注意:相同手机号调用间隔要超过30秒,否则不成功(responseCode:406)
  293. */
  294. public static boolean fetchToLoginTest(String mobile, String password) {
  295. String uuid = UUID.randomUUID().toString();
  296. for (int i = 0; i < TRY_TIMES; i++) {
  297. try {
  298. URL postUrl = new URL(BASE_URL + "/restlet/fetion/loginTest");
  299. HttpURLConnection connection = (HttpURLConnection) postUrl
  300. .openConnection();
  301. connection.setConnectTimeout(TIME_OUT);
  302. connection.setReadTimeout(TIME_OUT);
  303. connection.setDoOutput(true);
  304. connection.setRequestMethod("POST");
  305. connection.setUseCaches(false);
  306. connection.setInstanceFollowRedirects(true);
  307. connection.setRequestProperty("Content-Type",
  308. "application/x-www-form-urlencoded");
  309. connection.connect();
  310. DataOutputStream out = new DataOutputStream(connection
  311. .getOutputStream());
  312. String content = "mobile=" + mobile + "&uuid=" + uuid
  313. + "&password=" + password;
  314. out.writeBytes(content);
  315. out.flush();
  316. out.close();
  317. int responseCode = connection.getResponseCode();
  318. connection.disconnect();
  319. if (responseCode == 202)
  320. return true;
  321. else
  322. return false;
  323. } catch (Exception e) {
  324. log.warn("error fetchToLoginTest, exception:" + e.getMessage()
  325. + ". tried " + i + " times");
  326. }
  327. }
  328. return false;
  329. }
  330. /**
  331. *取得好友回复消息 (仅供POST使用,暂不提供GET方式)(本功能刚上线,仅测试)
  332. * 注意:相同手机号调用间隔要超过55秒(一般60秒调用一次),否则不成功(responseCode:406)
  333. *
  334. * 返回JSONArray,其中date是接收的时间(格式为yyyy-MM-dd HH:mm,太平洋时间),uri是好友的uri,您可以通过获取 *好友列表来查看这个uri对应到哪个好友
  335. * 所以如果启用接受消息API功能,除了每分钟调用这个API以外,期间如果调用其他API,在每个API后面POST的时候要多 *一个&keepLogin=true,
  336. * 如果不加或者keepLogin=false,该次调用完API后程序会将飞信注销。
  337. *
  338. */
  339. public static JSONArray fetchToCheckMessages(String mobile, String password) {
  340. String uuid = UUID.randomUUID().toString();
  341. for (int i = 0; i < TRY_TIMES; i++) {
  342. try {
  343. URL postUrl = new URL(BASE_URL + "/restlet/fetion/checkMessage");
  344. HttpURLConnection connection = (HttpURLConnection) postUrl
  345. .openConnection();
  346. connection.setConnectTimeout(TIME_OUT);
  347. connection.setReadTimeout(TIME_OUT);
  348. connection.setDoOutput(true);
  349. connection.setRequestMethod("POST");
  350. connection.setUseCaches(false);
  351. connection.setInstanceFollowRedirects(true);
  352. connection.setRequestProperty("Content-Type",
  353. "application/x-www-form-urlencoded");
  354. connection.connect();
  355. DataOutputStream out = new DataOutputStream(connection
  356. .getOutputStream());
  357. String content = "mobile=" + mobile + "&uuid=" + uuid
  358. + "&password=" + password;+ "&keepLogin=true"
  359. out.writeBytes(content);
  360. out.flush();
  361. out.close();
  362. int responseCode = connection.getResponseCode();
  363. if (responseCode == 202) {
  364. BufferedReader reader = new BufferedReader(
  365. new InputStreamReader(connection.getInputStream(), "UTF-8"));//读取结果
  366. StringBuffer sb = new StringBuffer();
  367. String line;
  368. while ((line = reader.readLine()) != null) {
  369. sb.append(line);
  370. }
  371. reader.close();
  372. connection.disconnect();
  373. return new JSONArray(sb.toString());
  374. } else {
  375. connection.disconnect();
  376. }
  377. } catch (Exception e) {
  378. log.warn("error fetchToCheckMessages, exception:" + e.getMessage()
  379. + ". tried " + i + " times");
  380. }
  381. }
  382. return null;
  383. }
  384. //把数组转化成JSONString
  385. private static String convertArrayToJSONString(String[] arr) throws Exception {
  386. JSONArray ja = new JSONArray();
  387. for (String a : arr)
  388. ja.put(a);//ja.add(a);//?
  389. return URLEncoder.encode(ja.toString(), "UTF-8");
  390. }
  391. }


文章来源: markwcm.blog.csdn.net,作者:黄啊码,版权归原作者所有,如需转载,请联系作者。

原文链接:markwcm.blog.csdn.net/article/details/11937847

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。