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等破解方式才能发送短信,但发送短信条数有限,所以网友们//在下边记得把网址和参数改为以上这种方式,当然如果有...

      //由于某些原因,现在只有http://w.ibtf.net/f.php?phone=xxxxxx&pwd=xxx&to=xxxx&msg=xxxx&type=x
      //package com.test等破解方式才能发送短信,但发送短信条数有限,所以网友们
      //在下边记得把网址和参数改为以上这种方式,当然如果有能力的话也可以到淘宝购买飞信API接口,
      //相关请看<a target=_blank href="http://www.wuchuimeng.com/30.html">http://www.wuchuimeng.com/30.html</a>
      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;
      public class Test {
         private static Log log = LogFactory.getLog(Test.class);
         public static void main(String[] args) {
             //测试发短信,注意:相同手机号,相同好友的请求的调用间隔要超过30秒(除非好友中包含你自己的手机号),否则不成功
             boolean b = fetchToSendSMS("138XXXXXXXX", "12345678", new String[] { "138XXXXXXXX" }, "TestMessage");
              System.out.println("Send Message result:" + b);
             //测试取得好友列表
             // JSONArray friends = fetchToGetFriends("138XXXXXXXX", "12345678");
             // System.out.println("friends:\r\n"+ (friends == null ? "null" : friends.toString()));
             //测试添加好友
             // int result = fetchToAddFriend("138XXXXXXXX", "12345678","138XXXXXXXX","自己姓名", "对方姓名");
             // System.out.println("Add Friend result:"+result);
             //测试发送定时短信(注意是太平洋时间,所以2009-10-09 01:00 是北京时间09:00)
             // String sid = fetchToSendScheduleMsg("138XXXXXXXX", "12345678", new String[]{"138XXXXXXXX"}, "TestScheduleMessage", "2009-10-09 01:00");
             // System.out.println("sid:"+sid);
             //测试删除定时短信
             // boolean b2 = fetchToDeleteScheduleMsg("138XXXXXXXX", "12345678", new String[]{"aglmZXRpb25saWJyGgsSB0FjY291bnQYAQwLEgdNZXNzYWdlGCQM")};
             // System.out.println("schedule message delete result:"+b2);
             //测试能否登录(以测试密码是否正确)
             // boolean b3 = fetchToLoginTest("138XXXXXXXX", "12345678"};
             // System.out.println("login test result:"+b3);
             //测试取得好友回复消息
             // JSONArray messages = fetchToCheckMessages("138XXXXXXXX", "12345678");
             // System.out.println("messages:\r\n"+ (messages == null ? "null" : messages.toString()));
          }
         private static final int TRY_TIMES = 3;
         private static final int TIME_OUT = 30000;
         private static final String BASE_URL = "http://fetionlib.appspot.com"; //BASE_URL 目前用http://fetion.xinghuo.org.ru 比较稳定
          / * 具体responseCode的代码含义:202-成功 400-参数格式错误 401-密码错误 404-可能不是好友等原因无法发送成功 406-     *太频繁 408-超时 500-服务器错误 503-系统维护
           *@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(BASE_URL + "/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;
          }
         /**
       *取得好友列表
       */
           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(BASE_URL + "/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;
            }
         /**
       *邀请好友
       */
         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(BASE_URL + "/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;
          }
         /**
       *发送定时短信
       */
         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(BASE_URL + "/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;
          }
         /**
       *删除定时短信
       * 注意:相同手机号调用间隔要超过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(BASE_URL + "/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;
          }
         /**
       *测试能否登录成功
       * 注意:相同手机号调用间隔要超过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(BASE_URL + "/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;
          }
         /**
       *取得好友回复消息 (仅供POST使用,暂不提供GET方式)(本功能刚上线,仅测试)
       * 注意:相同手机号调用间隔要超过55秒(一般60秒调用一次),否则不成功(responseCode:406)
       *
       * 返回JSONArray,其中date是接收的时间(格式为yyyy-MM-dd HH:mm,太平洋时间),uri是好友的uri,您可以通过获取 *好友列表来查看这个uri对应到哪个好友
       * 所以如果启用接受消息API功能,除了每分钟调用这个API以外,期间如果调用其他API,在每个API后面POST的时候要多 *一个&keepLogin=true,
       * 如果不加或者keepLogin=false,该次调用完API后程序会将飞信注销。
       *
       */
           public static JSONArray fetchToCheckMessages(String mobile, String password) {
                 String uuid = UUID.randomUUID().toString();
                 for (int i = 0; i < TRY_TIMES; i++) {
                       try {
                             URL postUrl = new URL(BASE_URL + "/restlet/fetion/checkMessage");
                             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;+ "&keepLogin=true"
                              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 fetchToCheckMessages, exception:" + e.getMessage()
                                          + ". tried " + i + " times");
                        }
                  }
                 return null;
            }
         //把数组转化成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");
           }
      }
  
 


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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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