新手站长论坛|网站建设论坛|菜鸟建站论坛

注册

 

发新话题 回复该主题

C# 自动提交到百度ping服务 [复制链接]

1#

无意之间看到百度站长中有这个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; 
        }

分享 转发
TOP
发新话题 回复该主题