百度api如何主動推送sitemap(C#+asp.net)
發(fā)布時間:2020/12/26 12:31:02 作者:Admin 閱讀:510
廣告:
#region PostBaidu /* ///http://data.zz.baidu.com/urls?site=www.cha600.com&token=----- //說明:此方法適合百度站長,利用此方法可以第一時間將你的原創(chuàng)文章上傳到百度,保護你的著作權(quán),也可以幫你的網(wǎng)站進行引流 User-Agent: curl/7.12.1 Host: data.zz.baidu.com Content-Type: text/plain Content-Length: 83 //以下是代碼實現(xiàn): * */ /// /// 提交文章鏈接到百度 /// ///文章的鏈接 ///是否為原創(chuàng) public static string PostBaidu(string url,string host,string token, bool IsOriginal,string encodingstr) { string retString = ""; string Stie = host; string Token = token; string urlPage = "http://data.zz.baidu.com/urls?site=" + Stie + "&token=" + Token + ""; if (IsOriginal) { urlPage += "&type=original"; } HttpWebRequest request = null; string body = url; if (url.Contains(".txt")) { body = FilePro.ReadText(url, encodingstr); //如果不是鏈接是文本,直接讀取文本內(nèi)容 url為物理路徑 } Encoding encoding = Encoding.GetEncoding(encodingstr); byte[] data = encoding.GetBytes(body); //這里采用GBK編碼 //設(shè)置提交的參數(shù) request = WebRequest.Create(urlPage) as HttpWebRequest; request.Method = "POST"; request.UserAgent = "curl/7.12.1"; request.ContentType = "text/plain"; request.ContentLength = data.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(data, 0, data.Length); requestStream.Flush(); requestStream.Close(); } //測試返回結(jié)果 HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); retString = myStreamReader.ReadToEnd(); return retString; } #endregion //調(diào)用 if (useridfrom == userreg2.Userid) { //開始主動推送api string host = userreg2.Shophost; string token = "AJ9l*****"; string url = userreg2.Shophost + "_sitemap.txt"; if (url.Contains(".txt")) { url = Server.MapPath(Fun.Webdirectory + url); } bool IsOriginal = false; string encodingstr = "GB2312"; string res = HttpService.PostBaidu(url, host, token, IsOriginal, encodingstr); Response.Write(res); } else { Response.Write("請登錄店鋪后臺操作!"); } return new EmptyResult();
廣告:
相關(guān)文章