创建一个通用类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) |微软学习