百度站长工具的主动推送功能,让新链接可以及时被百度收录。
C#实现如下
public static string sendUrl(string[] urls)
{
try
{
string formUrl = "http://data.zz.baidu.com/urls?site=www.site.com&token=code";
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;
}
}
调用方法
string info = sendUrl(new string[] { "http://www.aa.com/1.php", "http://www.aa.com/2.php" , "http://www.aa.com/3.php"});
返回值说明
是{"remain":8,"success":3} 表示已经推送成功,还剩8条可以推送,本次已经推送成功3条
异常返回码信息表:
200 无使用方式错误,需要进一步观察返回的内容是否正确
400 必选参数未提供
405 不支持的请求方式,我们只支持POST方式提交数据
411 HTTP头中缺少Content-Length字段
413 推送的数据过大,超过了10MB的限制
422 HTTP头中Content-Length声明的长度和实际发送的数据长度不一致
500 站长平台服务器内部错误