程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 自動提交到百度ping服務

C# 自動提交到百度ping服務

編輯:C#入門知識

今天無意之間看到百度站長中有這個ping服務(孤陋寡聞呀)....

那什麼什麼是Ping服務

ping是基於XML_RPC標准協議的更新通告服務,用於博客把內容更新快速通知給百度,以便百度及時進行抓取和更新。

看著是簡單的http post 提交.但是自己用 WebRequest 模擬時.死活返回操作超時.簡直無語.

下面百度ping 服務的例子(注意紅色部分.現在http的版本是1.1了.就是這個細節導致無法提交),順便說說Fiddler 這個工具確實很好用.

 

weblogUpdates.extendedPing xml-rpc請求舉例:
POST /ping/RPC2 HTTP/1.0
User-Agent: request
Host: ping.baidu.com
Content-Type: text/xml
Content-Length: 511

<?xml version="1.0" encoding="UTF-8"?><methodCall>
    <methodName>weblogUpdates.extendedPing</methodName>
    <params>
        <param>
            <value><string>百度的空間</string></value>
        </param>
        <param>
            <value><string>http://hi.baidu.com/baidu/</string></value>
        </param>
        <param>
            <value><string>http://baidu.com/blog/example.html</string></value>
        </param>
        <param>
            <value><string>http://hi.baidu.com/baidu/rss</string></value>
        </param>
    </params>
</methodCall>下面放出我自己的簡單例子(已經通過測試了)


      public void postToPing( )
        {
            try
            {
                string posturl = "http://ping.baidu.com/ping/RPC2"; //post 提交地址
                string refurl = "http://www.weletgo.com/"; //這裡可以隨便填寫.
                string content_type = "text/xml";        //提交類型.這裡一定要text/xml
                string postdt = postdata();              //提交數據
                string str = baiduping(posturl, postdt, content_type, refurl, false, Encoding.UTF8);
                Stream sm = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(str)); //下面這裡檢測提交是否成功
                XElement xle = XElement.Load(sm);
                var query = xle.Descendants("int");
                if (query.Count() > 0)
                {
                    string _val = query.ElementAtOrDefault(0).Value;
                    if (_val == "1")
                    {
                        Console.WriteLine("失敗");
                    }
                    else
                    {
                        Console.WriteLine("成功");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

               // log.Error(ex.Message);
            }
        }
        private string postdata()
        {
            //注意xml拼接的時候,xml的第一行的開頭必須不能有空格等 //下面直接是引用百度的例子
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>");
            sb.AppendLine(" <methodName>weblogUpdates.extendedPing</methodName>");
            sb.AppendLine("<params>");
            sb.AppendLine(" <param> ");
            sb.AppendLine("<value><string>百度的空間</string></value>");
            sb.AppendLine(" </param> ");
            sb.AppendLine(" <param>");
            sb.AppendLine("<value><string>http://hi.baidu.com/baidu/</string></value>");
            sb.AppendLine("</param>");
            sb.AppendLine("<param>");
            sb.AppendLine(" <value><string>http://baidu.com/blog/example.html</string></value>");
            sb.AppendLine(" </param>");
            sb.AppendLine(" <param> ");
            sb.AppendLine("<value><string>http://hi.baidu.com/baidu/rss</string></value>");
            sb.AppendLine("</param> ");
            sb.AppendLine("</params>");
            sb.AppendLine("</methodCall>");
            return sb.ToString().Trim();
        }

        private string baiduping(string targetURL, string formData, string contentType, string referer, bool allowAutoRedirect, Encoding ed)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(formData);
            //請求目標網頁
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
            request.Method = "POST";    //使用post方式發送數據
            request.UserAgent = "request";
            request.Referer = referer;
            request.ProtocolVersion = new Version("1.0");  //注意這裡這個版本好.一定要設置.現在默認提交是1.1了.否則會一直提示504
            request.ContentType = contentType == "" ? "application/x-www-form-urlencoded" : contentType;
            request.Timeout = 1000 * 10;
            request.ContentLength = data.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            string html = new StreamReader(stream, ed).ReadToEnd();
            return html; 
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved