`
mengdejun
  • 浏览: 405365 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Google App Engine 飞信API接口

    博客分类:
  • Sys
阅读更多

Google App Engine 飞信API接口

 

网页版飞信:https://fetionlib.appspot.com/

 

 

Google App Engine 飞信API接口

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 Terry Email: yaoxinghuo at 126 dot com
 *@version create: Aug 5, 2009 11:17:23 AM
 *@version update: Oct 15, 2009 00:11:00 AM
 *@description 飞信API(fetionlib) HTTP简单调用举例 另有Restlet的方式可供调用。网页版飞信首页:https://fetionlib.appspot.com/
 *              小提示:免费开通飞信:如果您的手机号没有开通飞信,可以到中国移动飞信网站查看开通,或者直接编辑短信KTFX发送到10086开通
 *              修改飞信密码:手机编辑新密码(6到16位,不能是中文或全角字符)内容发送到12520050
 *              直接在浏览器里输入以下地址(您的手机号码和密码请自行更改,密码不要包含/,朋友号码请填写自己的手机号码):
 *              https://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例子,其他语言类似
 *              如有疑问或对API的接口调用方式有任何更好的建议,欢迎提出宝贵意见
 * 
 *              现已经更新支持取得好友列表、POST方式的群发(8个或8个以下好友)和定时发送群发(定时群发最多30个好友),请看更新的例子
 * 
 *              更新近期发现有人利用本程序给他人发送轰炸短信,给他人造成严重骚扰,同时也大量消耗本站资源,已作如下限制:
 *              同一个手机号给同一个好友的发短信API以及其他的API(如:添加好友、获取好友列表等)请求间隔为30秒,30秒内的类似请求将无法完成。
 *              注:考虑到实际需要,给自己发送短信(手机号和对方好友号码相同或者群发好友里面包含自己手机号)的API请求将不会有30秒时间间隔的限制!
 * 
 *              本飞信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","TestMyName", "TestFriendName");
        // 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);
    }

    private static final int TRY_TIMES = 3;
    private static final int TIME_OUT = 30000;

    /**
     *发送短消息 更简单的Get方式(不支持群发,如要群发用下面POST方式,已更新),直接在浏览器里输入以下地址,手机号码和密码请自行改掉:
     * https://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","13916416465","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(
                        "https://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方式为:
       * https://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中显示!
       * 不成功返回空白
       * 注意:相同手机号调用间隔要超过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(
                                    "https://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())); // 读取结果
                              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方式为:
     * https://fetionlib.appspot.com/restlet/fetion/friend/13812345678/password/13912345678/MyName/FriendNickname 返回数字-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(
                        "https://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())); // 读取结果
                    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方式为(不支持群发,如要群发用下面POST方式,已更新):
     * https://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(
                        "https://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())); // 读取结果
                    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方式为:
     * https://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(
                        "https://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;
    }

    //把数组转化成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");
     }

}
 

分享到:
评论

相关推荐

    google app Engine for java中文api

    ### Google App Engine for Java中文API知识点详解 #### 一、Google App Engine for Java概览 - **背景介绍**: - Google App Engine最初是专为Python开发者设计的平台,但随着时间的发展,为了满足更广泛开发者...

    Google App Engine API 大全

    **Google App Engine (GAE) API 大全** Google App Engine 是一个托管平台,它允许开发者使用特定的API和框架来构建、部署和运行Web应用程序。这个平台支持多种编程语言,其中Java是其中之一。在本指南中,我们将...

    云端代码Google App Engine编程指南 高清带书签

    在谈论《云端代码Google App Engine编程指南》之前,我们需要先了解一下Google App Engine(GAE)平台的背景知识。Google App Engine(简称GAE)是由Google公司提供的一个无服务器(serverless)的云计算平台,该...

    Google App Engine搭建Twitter API Proxy

    ### Google App Engine 搭建 Twitter API Proxy #### 一、什么是 Twitter API Proxy? Twitter API Proxy 实质上是一种代理服务,它允许用户通过第三方应用程序或编程语言调用 Twitter 的 REST API。Twitter 的 ...

    appengine-java-sdk-1.3.1 GoogleApp开发的SDK(Java版)

    这个SDK包含了运行和测试Google App Engine应用所需的所有组件,包括开发服务器、API库、文档和其他资源。 Google App Engine是一个平台即服务(PaaS)产品,它允许开发者构建、运行和维护Web应用程序,无需考虑...

    Google App Engine 入门教程

    Google App Engine(GAE)是由Google公司提供的一个为开发者提供网站托管服务的平台。它允许开发者运行应用程序在Google的基础设施上。GAE提供了包括Python、Java在内的多种语言支持,并且提供了数据存储、API调用、...

    Google App Engine Documentation

    在“google-appengine-docs-20081003”这个压缩包中,可能包含了早期版本(2008年10月3日)的Google App Engine开发指南、API参考、最佳实践和示例代码等内容。这些文档帮助开发者了解如何利用Google App Engine构建...

    修改appengine-tools-api.jar文件,解决jsp乱码

    1、用winrar找开eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.2.0.v200904062254\appengine-java-sdk-1.2.0\lib\appengine-tools-api.jar。 用你下载的包中Application.class替换om.google.appengine....

    Google App Engine 最新版 (google_appengine_1.9.91.zip)

    在搭建WebRTC(AppRTC)时,我们会遇到需要部署Google App Engine的难题,并且最近墙的厉害,所以我特地把我珍藏的Google App Engine 最新版(2020-7-5)分享出来,希望可以方便想搭建WebRTC(AppRTC)的同学们

    google app engine示例源代码

    clock.rar 这是我今天做的一个基于google app engine写的一个在该平台上开发app的示例,通过该示例的学习,我们可以很快上手学会怎样在GAE上开发属于你的应用程序了,希望对大家有用!

    Google App Engine文档

    **Google App Engine**是Google提供的一种云计算平台,用于构建和托管Web应用程序。它允许开发者使用Python、Java、Go或PHP等语言编写应用,并在Google的基础设施上运行,无需关心服务器维护、扩展性和高可用性等...

    GoogleAppEngine api 手册

    **Google App Engine API 手册** Google App Engine 是一个由 Google 提供的平台,用于构建和托管基于云计算的应用程序。这个平台提供了丰富的服务,让开发者能够利用 Google 的基础设施来运行他们的应用程序,无需...

    Google Appengine应用文档

    Google App Engine 是一个由 Google 提供的平台,允许开发者构建并运行基于 Web 的应用程序。这个平台提供了服务器、数据库和其他服务,使得开发者无需关心底层基础设施的管理,而专注于应用的开发。本文档将详细...

    appengine-java-sdk

    2. **API接口**:SDK包含了一系列的Java库,这些库提供了与AppEngine服务交互的接口,例如数据存储服务(Datastore)、任务队列(Task Queue)、邮件服务(Mail Service)等。 3. **数据存储**:AppEngine使用NoSQL...

    Google App Engine 开发包(part1,共2部分)

    Eclipse Google 插件 Google App Engine SDK for Java Google App Engine文档 按钮 Google App Engine 开发包

    Using Google App Engine

    ### 使用Google App Engine:深入解析与实践 #### 核心知识点概述 Google App Engine(GAE)是Google推出的一款用于构建和托管Web应用的平台服务,它为开发者提供了丰富的工具和资源,使得开发者能够轻松地在...

    Google App Engine的一些限制

    ### Google App Engine的一些限制 Google App Engine (GAE) 是Google Cloud Platform的重要组成部分之一,它为开发者提供了构建高效、可扩展的应用程序的平台。本文旨在详细介绍GAE在资源使用方面的一些关键限制,...

Global site tag (gtag.js) - Google Analytics