⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlrpcpings.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;using System.IO;using System.Net;using System.Text;using System.Threading;using System.Xml;using CommunityServer.Components;namespace CommunityServer.Blogs.Components
{

    /// <summary>
    /// Enables pinging of community blogging sites such as weblogs.com after 
    /// the blog is updated.
    /// </summary>
    [Serializable]
    public class XmlRpcPings
    {
        private Weblog weblog = null;
        public XmlRpcPings(Weblog wl)
        {
            weblog = wl;
        }

        public bool CanPing
        {
            get { return (weblog != null && weblog.EnablePings && weblog.PingUrls != null && weblog.PingUrls.Length > 0); }
        }

        /// <summary>
        /// Each ping has taken up to a minute to complete. This overload of SendPings enables
        /// SendPings to be executed on a seperate thread.
        /// </summary>
        /// <param name="useBackGroundThread">Should this operation happen on a new thread</param>
        public static void SendPings(Weblog wl,bool useBackGroundThread)
        {
            XmlRpcPings pinger = new XmlRpcPings(wl);
            if (useBackGroundThread)
            {
                ManagedThreadPool.QueueUserWorkItem(new WaitCallback(pinger.SendPings));
            }
            else
            {
                pinger.SendPings();
            }
        }

        public void SendPings(object state)
        {
            SendPings();
        }

        public  void SendPings()
        {

            //Can we proceed?
            if (!CanPing)
            {
                return;
            }

            string url = BlogUrls.Instance().HomePage(weblog.ApplicationKey);
            string name = weblog.Name;

            //Generate the body of our Post
            StringWriter sw = new StringWriter();
            XmlTextWriter xmlWriter = new XmlTextWriter(sw);
            xmlWriter.Formatting = Formatting.Indented;
            //xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("methodCall");
            xmlWriter.WriteElementString("methodName", "weblogUpdates.ping");
            xmlWriter.WriteStartElement("params");
            xmlWriter.WriteStartElement("param");
            xmlWriter.WriteElementString("value",  name);
            xmlWriter.WriteEndElement(); //close param
            xmlWriter.WriteStartElement("param");
            xmlWriter.WriteElementString("value", url);
            xmlWriter.WriteEndElement();  //close param
            xmlWriter.WriteEndElement(); //close params
            xmlWriter.WriteEndElement(); //close methodCall

            
            //Get a byte array to write to the Request Stream.
            byte[] content = Encoding.UTF8.GetBytes(sw.ToString());
            sw.Close();
            xmlWriter.Close();

            HttpWebRequest request = null;
            foreach (string pingUrl in weblog.PingUrls)
            {
                try
                {
                    request = CSRequest.CreateRequest(pingUrl,url);
                    request.Timeout = 60000;
                    request.Method = "POST";
                    request.ContentLength = content.Length;
                    request.ContentType = "text/xml";
                    request.KeepAlive = false;

                    using(Stream st = request.GetRequestStream())
                    {
                        st.Write(content, 0, content.Length);
                        st.Close();

                        using(WebResponse response = request.GetResponse())
                        {

                            //If we process the responses, comment out the following line
                            response.Close();
                        }
                    }

                    //We could track this, but for now we will ignore the responses
                    //StreamReader sr = new StreamReader(response.GetResponseStream());
                }
                    //eat the exception. This happens in the background on a seperate thread
                    //if we add logging, this should be tracked.
                catch { } 
            }
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -