seo-百度(baidu),必应(bing) api 普通收录

创建一个通用类SeoUrlSend
package leyan.website.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;


public class SeoUrlSend {
//postUrl-百度,bing等提交地址 sendUrl-需要收录的地址
    public String Post(String postUrl , String sendUrl) {
        StringBuilder result = new StringBuilder();
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            URL realUrl;
            HttpURLConnection conn;
            realUrl = new URL(postUrl);
            conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Charset", "UTF-8");

            conn.setDoOutput(true);
            conn.setDoInput(true);

            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
            out.print(sendUrl);
            out.flush();

            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result.toString();
    }

}

调用通用方法发送收录网址
public void seoPost(String sendUrl){
        //百度收录代码
        SeoUrlSend seoUrlSendBaiDu = new SeoUrlSend();
        String postUrlBaiDu = "http://data.zz.baidu.com/urls?site=***&token=***";
        seoUrlSendBaiDu.Post(postUrlBaiDu, sendUrl);
        //必应收录代码
        SeoUrlSend seoUrlSendBing = new SeoUrlSend();
        String postUrlBing = "https://ssl.bing.com/webmaster/api.svc/json/SubmitUrl?apikey=***";
        //siteUrl为网站网址,例如:http://example.com,sendUrl为要提交的网址,例如:http://example.com/index.html
        String sendUrlBing = "{\"siteUrl\":\"https://www.xxx.com\",\"url\":\""+sendUrl+"\"}";
        seoUrlSendBing.Post(postUrlBing, sendUrlBing);
    }
调用seoPost方法代入参数即可实现收录

必应文档参考:IWebmasterApi.SubmitUrl(String, String) Method (Microsoft.Bing.Webmaster.Api.Interfaces) |微软学习

百度文档参考:https://ziyuan.baidu.com/linksubmit/index

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值