百度站長工具的主動推送功能,以最為快速的提交方式,推薦您將站點當天新產(chǎn)出鏈接立即通過此方式推送給百度,以保證新鏈接可以及時被百度收錄
官方提供了curl、post、php、ruby的實現(xiàn)示例,并沒有C#的官方示例。既然提供了post的方式,那么就可以用C#實現(xiàn),下面是實現(xiàn)代碼:
ASP.net百度主動推送代碼范例
public static string PostUrl(string[] urls)
{
try
{
string formUrl = "
http://data.zz.baidu.com/urls?site=www.yoursite.com&token=yourcode";
string formData = "";
foreach (string url in urls)
{
formData += url + "\n";
}
byte[] postData = System.Text.Encoding.UTF8.GetBytes(formData);
// 設置提交的相關參數(shù)
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;
// 提交請求數(shù)據(jù)
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;
}
}
調(diào)用的時候,把您的網(wǎng)址傳入
string info = PostUrl(new string[] { "http://www.jb51.net/article/1.html", "http://www.jb51.net/article/2.html" });
返回的結果是{"remain":498,"success":2} 表示已經(jīng)推送成功,還剩498條可以推送,本次已經(jīng)推送成功2條。
另外附上可能出現(xiàn)的異常情況的返回碼信息,供調(diào)試用:
200 無使用方式錯誤,需要進一步觀察返回的內(nèi)容是否正確
400 必選參數(shù)未提供
405 不支持的請求方式,我們只支持POST方式提交數(shù)據(jù)
411 HTTP頭中缺少Content-Length字段
413 推送的數(shù)據(jù)過大,超過了10MB的限制
422 HTTP頭中Content-Length聲明的長度和實際發(fā)送的數(shù)據(jù)長度不一致
500 站長平臺服務器內(nèi)部錯誤
我實際應用代碼
//點擊按鈕觸發(fā)
protected void Button1_Click(object sender, EventArgs e)
{
string info = PostUrl(new string[] { "http://www.jb51.net/", "http://www.jb51.net/article/3.html" });
this.Label1.Text= info;
}
到此這篇關于ASP.net百度主動推送功能實現(xiàn)代碼的文章就介紹到這了,更多相關ASP.net百度主動推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
來源:腳本之家
鏈接:https://www.jb51.net/article/195605.htm
申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!