优点是快

不需要页面执行,,发布文章之后立即推送,所以,不管有没有人访问,都可以自动实时推送

尝试了一下httpclient,没找到相关资料,post方式无法塞url进去

最后改为

import java.net.URL;
import java.net.URLConnection;

文章发布后,调用saveOrUpdate方法,入库之后调用推送

private void activelyPush(Session session, ArticleModel articleModel) throws IOException {

        ActivelyPushDao apd = new ActivelyPushDao();
        List<ActivelyPush> activelyPushs = apd.getAll(session);//获取所有要推送的搜索引擎
        String[] Parameters = {
            "你的网站/" + articleModel.getAuthor_column_py() + "/" + articleModel.getId() + ".html"
        };
        for (ActivelyPush ap : activelyPushs) {
            String result = "";
            PrintWriter out = null;
            BufferedReader in = null;
            try {
                //建立URL之间的连接
                URL url = new URL(ap.getUrl());
                URLConnection conn = url.openConnection();
                //设置通用的请求属性
                conn.setRequestProperty("Host", "data.zz.baidu.com");
                conn.setRequestProperty("User-Agent", "curl/7.12.1");
                conn.setRequestProperty("Content-Length", "83");
                conn.setRequestProperty("Content-Type", "text/plain");
                //发送POST请求必须设置如下两行
                conn.setDoInput(true);
                conn.setDoOutput(true);
                //获取conn对应的输出流
                out = new PrintWriter(conn.getOutputStream());
                //发送请求参数
                String param = "";
                for (String s : Parameters) {
                    param += s + "\n";
                }
                out.print(param.trim());
                //进行输出流的缓冲
                out.flush();
                //通过BufferedReader输入流来读取Url的响应
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }

            } catch (Exception e) {
                System.out.println("发送post请求出现异常!" + e);
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }
02-13 08:40