代码下载:http://www.zuidaima.com/share/1822672957737984.htm
原文:seo优化之Google和Baidu Ping服务实现快速收录文章的java,php代码实现
最近在做关于google和百度的ping服务,希望能提高搜索引擎蜘蛛的抓取频率,搜索了大半天都不太好用,所以自己写了一份,大家可以参考下:
package com.zuidaima.core.util; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; public class Ping { public static final String BAIDU_RPC = "http://ping.baidu.com/ping/RPC2"; public static final String GOOGLE_RPC = "http://blogsearch.google.com/ping/RPC2"; private static String buildMethodCall(String title, String url, String shareURL, String rssURL) { StringBuffer buffer = new StringBuffer(); buffer.append(""); buffer.append(""); buffer.append(" "); return buffer.toString(); } public static String pingBaidu(String title, String url, String shareURL, String rssURL) throws Exception { PostMethod post = new PostMethod(BAIDU_RPC); post.addRequestHeader("User-Agent", "request"); post.addRequestHeader("Content-Type", "text/xml"); String methodCall = buildMethodCall(title, url, shareURL, rssURL); RequestEntity entity = new StringRequestEntity(methodCall, "text/xml", "utf-8"); post.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); // httpclient.getHostConfiguration().setProxy("127.0.0.1", 8888); httpclient.executeMethod(post); String ret = post.getResponseBodyAsString(); post.releaseConnection(); return ret; } public static String pingGoogle(String title, String url, String shareURL, String rssURL) throws Exception { PostMethod post = new PostMethod(GOOGLE_RPC); post.addRequestHeader("User-Agent", "request"); post.addRequestHeader("Content-Type", "text/xml"); String methodCall = buildMethodCall(title, url, shareURL, rssURL); RequestEntity entity = new StringRequestEntity(methodCall, "text/xml", "utf-8"); post.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); // httpclient.getHostConfiguration().setProxy("127.0.0.1", 8888); httpclient.executeMethod(post); String ret = post.getResponseBodyAsString(); post.releaseConnection(); return ret; } public static void main(String[] args) throws Exception { String ret = Ping.pingBaidu("最代码", "http://www.zuidaima.com/", "http://www.zuidaima.com/share/1787210045197312.htm", "http://www.zuidaima.com/share/rss.htm"); System.out.println(ret); ret = Ping.pingGoogle("最代码", "http://www.zuidaima.com/", "http://www.zuidaima.com/share/1787210045197312.htm", "http://www.zuidaima.com/share/rss.htm"); System.out.println(ret); } }weblogUpdates.extendedPing "); buffer.append(""); buffer.append(" "); buffer.append(""); buffer.append(" " + url + " "); buffer.append(" " + title + " "); buffer.append(" " + shareURL + " "); buffer.append(" " + rssURL + "
运行截图如下:
0 flerror 0 message Thanks for the ping.
另外找了份php的代码,附上来给大家做参考,有需要的下载吧
/** +------------------------------------------------------------------------------ * 通知搜索引擎过来抓去最新发布的内容。秒收不是梦 * 目前仅支持Google和Baidu +------------------------------------------------------------------------------ */ class ping { public $method, $callback; public function method($site_name, $site_url, $update_url, $update_rss) { $this->method = ""; return $this->method; } public function _post($url, $postvar) { $ch = curl_init(); $headers = array( "POST " . $url . " HTTP/1.0", "Content-type: text/xml;charset="utf-8"", "Accept: text/xml", "Content-length: " . strlen($postvar) ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar); $res = curl_exec($ch); curl_close($ch); return $res; } public function google() { $this->callback = $this->_post('http://blogsearch.google.com/ping/RPC2', $this->method); return strpos($this->callback, " weblogUpdates.extendedPing {$site_name} {$site_url} {$update_url} {$update_rss} 0 ") ? true : false; } public function baidu() { $this->callback = $this->_post('http://ping.baidu.com/ping/RPC2', $this->method); return strpos($this->callback, "0 ") ? true : false; } }