Java 以 post 发送方式实现百度链接提交主动推送

16人浏览 / 0人评论

我的博客自从换了系统之后,百度一直不收录,然后试试post请求这个方法看看有没有效果

推送接口:

 

import com.spellbed.mapper.HouseResourceMapper;
import com.spellbed.util.MD5Util;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 东泰 http://dt2008.cn
 * 2021-7
 */
public class Demo {

    //接口调用地址
    private static final String URL="http://data.zz.baidu.com/urls?site=https://dt2008.cn&token=xxxxxxxxx";

    public static void main(String []args) {
        String site="https://dt2008.cn";  //改为自己站点的site值
        String token="xxxxxxxxx";   //改为自己站点的token
        //需要提交的资源链接
        String []urlsArr= {
                "https://dt2008.cn/article/275.html",
                "https://dt2008.cn/article/274.html",
                "https://dt2008.cn/article/273.html",
        };
        //将urlsArr数组转化为字符串形式
        String urlsStr=urlsArrToString(urlsArr);
        //打印结果
        System.out.println(Post(site,token,urlsStr));
    }
    public static String Post(String site,String token,String urlsStr) {
        String result="";
        PrintWriter out=null;
        BufferedReader in=null;
        try {
            //建立URL之间的连接
            URLConnection conn = new URL(URL+"?site="+site+"&token="+token).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());

            out.print(urlsStr);
            //进行输出流的缓冲
            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);
        } finally{
            try{
                if(out != null){
                    out.close();
                }
                if(in!= null){
                    in.close();
                }

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

    //Array转String
    public static String urlsArrToString(String []urlsArr) {
        String tempResult = "";
        for (int i = 0; i < urlsArr.length; i++) {
            if (i == urlsArr.length - 1) {
                tempResult += urlsArr[i].trim();
            } else {
                tempResult += (urlsArr[i].trim() + "\n");
            }
        }
        return tempResult;

    }
}

全部评论