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