百度链接提交主动推送 c#实现

     说明:此方法适合百度站长,利用此方法可以第一时间将你的原创文章上传到百度,保护你的著作权,也可以帮你的网站进行引流

     以下是代码实现:

/// <summary>
        /// 提交文章链接到百度
        /// </summary>
        /// <param name="url">文章的链接</param>
        /// <param name="IsOriginal">是否为原创</param>
        public static void PostBaidu(string url, bool IsOriginal)
        {
            string Stie = "www.example.com";
            string Token = "你的准入秘钥";
            string urlPage = "http://data.zz.baidu.com/urls?site=" + strStie + "&token=" + strToken + "";
            if (IsOriginal)
            {
                urlPage += "&type=original";
            }            
            HttpWebRequest request = null;
            Encoding encoding = Encoding.GetEncoding("GBK");
            byte[] data = encoding.GetBytes(url);               //这里采用GBK编码
 
            //设置提交的参数         
            request = WebRequest.Create(urlPage) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/plain";
            request.ContentLength = data.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
                requestStream.Flush();
                requestStream.Close();
            } 

//测试返回结果 
            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"));
            string retString = myStreamReader.ReadToEnd();
           }

    


posted @ 2016-03-23 15:12  NightKey  阅读(832)  评论(0编辑  收藏  举报