打赏

网站监测短信、微信提醒

网站预警是必不可少的,以前做过用javamail邮件接收提示消息,现在大家都玩微信,邮件方式感觉有点out了.今天唐老大发了个接口,去官网研究了一下,可以用短信、微信接收一些网站信息

不知其他更好的方法,就用了httpclient,略显笨重.欢迎交流

废话不多说了,直接说使用方式

 

1.注册来信码:@官网

注册后

1.1,开发者选项-开发设置,获取accesskey\secretkey

1.2,融合通讯-IM及时通讯,开启IM

2.微信关注公众号"来信码",手机绑定

3.代码

 

package yanantest;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestImlaixin {
    public static void main(String[] args) {
        //测试时请将替换你的useraccesskey、、usersecretkey、、mobile
        //url1和url2作用一样,个人猜测url1是现在的语法,url2是老版本。get和post结果一样。httpclient个人学的不精通,不推荐哪种方式了
        //String url1="https://imlaixin.cn/Api/send/data/json?accesskey=useraccesskey&secretkey=usersecretkey&mobile=1******76**&content=你好,world";
        //String url2="http://sms.bechtech.cn/Api/send/data/json?accesskey=useraccesskey&secretkey=usersecretkey&mobile=1******76**&content=你好,world";
        //testGet(url1);
        //testPost(url2);
    }
    @SuppressWarnings("finally")
    public static String testGet(String url){
        @SuppressWarnings({ "resource", "deprecation" })
        HttpClient httpClient=new DefaultHttpClient();
        HttpGet get= new HttpGet(url);
        get.setHeader("User-Agent", "Mozilla/5.0");
        String string="";
        try {
            HttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            string = EntityUtils.toString(entity, "UTF-8");
            System.out.println(response.getStatusLine().getStatusCode());
            get.releaseConnection();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            return string;
        }
    }@SuppressWarnings("finally")
    public static String testPost(String url){
        @SuppressWarnings({ "resource", "deprecation" })
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost post=new HttpPost(url);
        StringEntity se = new StringEntity("", "UTF-8");
        post.setHeader("User-Agent", "Mozilla/5.0");
        post.setEntity(se);
        String string="";
        try {
            HttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            string = EntityUtils.toString(entity, "UTF-8");
            System.out.println(response.getStatusLine().getStatusCode());
            post.releaseConnection();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            return string;
        }
    }
}
View Code

 

 

PS:20180128:此应用已不能用,发不了短信,也不给你退款,也没客服,官网电话都打不通,大家注意确认

posted @ 2017-04-05 16:00  每天都要学一点  阅读(466)  评论(0编辑  收藏  举报