代码实现百度的自动Ping服务
为了加快网站页面收录,seo人员可通过网站外链建设来实现,seo外链专员就是做这个工作的。还有一种方式是向搜索引擎提交网站的URL,这就是百度的Ping服务。
ping是基于XML_RPC标准协议的更新通告服务,它能快速的将网站内容的更新通知百度,便于百度及时抓取。详细的协议规则请参考什么是Ping服务。
百度的Ping服务给我们提供了两种方式,一是手动Ping,链接地址是http://ping.baidu.com/ping.html,操作界面如下图
二是自动Ping服务,需要通过代码去实现,已经有很多的博客系统都自带了这项功能,曾经在一套C#开源系统中也实现了自动Ping百度,代码展示如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace YCYcms.Common { /// <summary> /// 百度相关类 /// </summary> public class BaiDu { /// <summary> /// 推送链接至百度 /// </summary> /// <param name="urls">链接集合</param> /// <returns></returns> public static string sendUrlToBaidu(string[] urls) { try { string formUrl = " http://data.zz.baidu.com/urls?site=www.qishunnet.com&token=FdSmdb3LRa4JitQp"; string formData = ""; foreach (string url in urls) { formData += url + "\n"; } byte[] postData = System.Text.Encoding.UTF8.GetBytes(formData); // 设置提交的相关参数 System.Net.HttpWebRequest request = System.Net.WebRequest.Create(formUrl) as System.Net.HttpWebRequest; System.Text.Encoding myEncoding = System.Text.Encoding.UTF8; request.Method = "POST"; request.KeepAlive = false; request.AllowAutoRedirect = true; request.ContentType = "text/plain"; request.UserAgent = "curl/7.12.1"; request.ContentLength = postData.Length; // 提交请求数据 System.IO.Stream outputStream = request.GetRequestStream(); outputStream.Write(postData, 0, postData.Length); outputStream.Close(); System.Net.HttpWebResponse response; System.IO.Stream responseStream; System.IO.StreamReader reader; string srcString; response = request.GetResponse() as System.Net.HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8")); srcString = reader.ReadToEnd(); string result = srcString; //返回值赋值 reader.Close(); return result; } catch (Exception ex) { return ex.Message; } } } }
这段代码就实现了百度的自动Ping服务,当新增或修改内容时,就可以调用这个方法通知百度,就是给百度传个话,我有更新内容了,有空就过来收录吧,当然最终能否收录,还取决于网站的其它诸多因素。