using System;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using Spaces;

public partial class RssProxy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Response.ContentType = "text/xml";
        int count=10;
        string url=Request.QueryString["url"];
        string c=Request.QueryString["n"];
        try
        {
            if (c != null)
                count = int.Parse(c);
        }
        catch { }
        if (url == null || url == string.Empty)
            return;
        
        XmlDocument doc = new XmlDocument();
        string enc;
        string xmText = GetPageText(url,out enc);
        if (enc != null || enc != string.Empty)
        {
            Response.ContentEncoding = System.Text.Encoding.GetEncoding(enc);
        }
   
        doc.LoadXml(xmText);
       
        if (doc.DocumentElement == null)
            return;
       
        XmlNode Rootnode = doc.DocumentElement;
        XmlNode channel =Rootnode.SelectSingleNode("channel");
        XmlNodeList xlist = Rootnode.SelectNodes("channel//item");
        if (channel["Count"]!=null)
        channel["Count"].InnerText = xlist.Count > count ? count.ToString() : xlist.Count.ToString();
        for (int i = 0; i < xlist.Count; i++)
        {
            if((i+1)>count)
            channel.RemoveChild(xlist[i]);
           
         

        }
        doc.Save(Response.Output);


      

    }
    #region 得到网络流
    public static HttpWebResponse GetResponse(string url)
    {
        return GetResponse(url, @"http://www.bfor.cn");
    }
    public static HttpWebRequest CreateRequest(string url)
    {
        return CreateRequest(url, @"http://www.bfor.cn");
    }
    public static HttpWebRequest CreateRequest(string url, string referral)
    {
        WebRequest req = WebRequest.Create(url);

        HttpWebRequest wreq = req as HttpWebRequest;
        if (null != wreq)
        {
            wreq.UserAgent = VersionInfo.UserAgent;
            wreq.Referer = @"http://www.bfor.cn";
            wreq.Timeout = 60000;
        }
        return wreq;
    }
    public static HttpWebResponse GetResponse(string url, string referral)
    {
        HttpWebRequest request = CreateRequest(url);
        return (HttpWebResponse)request.GetResponse();
    }
    public static string GetPageText(string url, out string referral)
    {
        string str;
        HttpWebResponse response = GetResponse(url);
        using (Stream s = response.GetResponseStream())
        {
            string enc = getEncoding(response); 
            if (enc == "")
                enc = "utf-8";
            referral = enc;
            Encoding encode = System.Text.Encoding.GetEncoding(enc);
            using (StreamReader sr = new StreamReader(s, encode))
            {
                str = sr.ReadToEnd();
            }
        }
        response.Close();
        return str;
    }

   
    private static string getEncoding(HttpWebResponse httpResp)
    {
        string contentType = httpResp.ContentType;//类似“Content-Type: text/html; charset=utf-8;”或“Content-Type: text/html; charset=utf-8”或者“Content-Type: text/html”//注意utf-8后面有可能没有分号
        int i = contentType.IndexOf("charset=");
        if (i >= 0)
        {
            i += 8;
            int j = contentType.IndexOf(';', i);
            if (j >= i)
            {
                return contentType.Substring(i, j - i).Trim();
            }
            return contentType.Substring(i);
        }
        return string.Empty;
    }
    #endregion

}
