NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public NextPVR Other Clients Old Stuff (legacy) NextPVR Enhanced Web Admin (NEWA) v
« Previous 1 … 5 6 7 8 9 … 47 Next »
StreamerPost.cs change to enable automatic compression/decompression (gzip) support

 
  • 0 Vote(s) - 0 Average
StreamerPost.cs change to enable automatic compression/decompression (gzip) support
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#1
2014-02-21, 06:42 PM
Additions in bold/red.

It's enabled by default and it can be turned off by using "streamerPost.EnableCompression = false;" after creating a new StreamerPost() instance.

Code:
using System;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using System.Collections.Specialized;

/// <summary>
/// This class is used to ease the process of posting commands to VLC that is running on the server.  It can
/// be reused for anything that needs to post to a different website
/// </summary>
///
namespace N_EWA.classes
{
    public class StreamerPost
    {
        /// <summary>
        /// determines what type of post to perform.
        /// </summary>
        public enum PostTypeEnum
        {
            /// <summary>
            /// Does a get against the source.
            /// </summary>
            Get,
            /// <summary>
            /// Does a post against the source.
            /// </summary>
            Post
        }

        private string m_url = string.Empty;
        private NameValueCollection m_values = new NameValueCollection();
        private PostTypeEnum m_type = PostTypeEnum.Get;
    [B][color=#FF0000]private bool m_UseCompression = true;[/color][/B]
        /// <summary>
        /// Default constructor.
        /// </summary>
        public StreamerPost()
        {
        }

        /// <summary>
        /// Constructor that accepts a url as a parameter
        /// </summary>
        /// <param name="url">The url where the post will be submitted to.</param>
        public StreamerPost(string url)
            : this()
        {
            m_url = url;
        }

        /// <summary>
        /// Constructor allowing the setting of the url and items to post.
        /// </summary>
        /// <param name="url">the url for the post.</param>
        /// <param name="values">The values for the post.</param>
        public StreamerPost(string url, NameValueCollection values)
            : this(url)
        {
            m_values = values;
        }

        /// <summary>
        /// Gets or sets the url to submit the post to.
        /// </summary>
        public string Url
        {
            get
            {
                return m_url;
            }
            set
            {
                m_url = value;
            }
        }
        /// <summary>
        /// Gets or sets the name value collection of items to post.
        /// </summary>
        public NameValueCollection PostItems
        {
            get
            {
                return m_values;
            }
            set
            {
                m_values = value;
            }
        }
        /// <summary>
        /// Gets or sets the type of action to perform against the url.
        /// </summary>
        public PostTypeEnum Type
        {
            get
            {
                return m_type;
            }
            set
            {
                m_type = value;
            }
        }
        [B][COLOR="#FF0000"]/// <summary>
        /// Gets or sets whether or not to enable automatic comression (deflate/gzip) handling.
        /// </summary>
        public bool EnableCompression
        {
            get
            {
                return m_UseCompression;
            }
            set
            {
                m_UseCompression = value;
            }
        }[/COLOR][/B]
        /// <summary>
        /// Posts the supplied data to specified url.
        /// </summary>
        /// <returns>a string containing the result of the post.</returns>
        public string Post()
        {
            StringBuilder parameters = new StringBuilder();
            for (int i = 0; i < m_values.Count; i++)
            {
                EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
            }
            string result = PostData(m_url, parameters.ToString());
            return result;
        }
        /// <summary>
        /// Posts the supplied data to specified url.
        /// </summary>
        /// <param name="url">The url to post to.</param>
        /// <returns>a string containing the result of the post.</returns>
        public string Post(string url)
        {
            m_url = url;
            return this.Post();
        }
        /// <summary>
        /// Posts the supplied data to specified url.
        /// </summary>
        /// <param name="url">The url to post to.</param>
        /// <param name="values">The values to post.</param>
        /// <returns>a string containing the result of the post.</returns>
        public string Post(string url, NameValueCollection values)
        {
            m_values = values;
            return this.Post(url);
        }
        /// <summary>
        /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
        /// </summary>
        /// <param name="postData">The data to post.</param>
        /// <param name="url">the url to post to.</param>
        /// <returns>Returns the result of the post.</returns>
        private string PostData(string url, string postData)
        {
            WebRequest.DefaultWebProxy = null;
            HttpWebRequest request = null;
            if (m_type == PostTypeEnum.Post)
            {
                Uri uri = new Uri(url);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;
        [B][COLOR="#FF0000"]//request.Proxy = null;
        if (m_UseCompression)
        {
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        }[/COLOR][/B]
                using (Stream writeStream = request.GetRequestStream())
                {
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] bytes = encoding.GetBytes(postData);
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }
            else
            {
                Uri uri = new Uri(url + "?" + postData);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";
        [B][COLOR="#FF0000"]//request.Proxy = null;
        if (m_UseCompression)
        {
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        }[/COLOR][/B]
           }
            string result = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// Encodes an item and ads it to the string.
        /// </summary>
        /// <param name="baseRequest">The previously encoded data.</param>
        /// <param name="dataItem">The data to encode.</param>
        /// <returns>A string containing the old data and the previously encoded data.</returns>
        private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
        {
            if (baseRequest == null)
            {
                baseRequest = new StringBuilder();
            }
            if (baseRequest.Length != 0)
            {
                baseRequest.Append("&");
            }
            baseRequest.Append(key);
            baseRequest.Append("=");
            baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
        }
    }

}
UncleJohnsBand
Offline

Posting Freak

U.S.A.
Posts: 5,643
Threads: 258
Joined: Feb 2005
#2
2014-02-22, 02:00 AM
Thanks... it will be in next build.
Intel Core i7 @ 4.00GHz Skylake 14nm
ASUSTeK COMPUTER INC. Z170-DELUXE
Windows 10 Pro x64
PVR Software: NPVR 5.1.1
SiliconDust HDHomeRun HDHR5-4US Connect Quatro 4 Channel Tuner
Roku Ultra
2 PCH A-100's
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#3
2014-02-22, 04:58 AM
OT but...

I've moved the Youtube parsing code back to parser.aspx for the next build (with updated parsing code). I found out why it was taking so long to return the results for me: .Net 4.5 on the server (Server 2012) causes it to take 2 minutes to return on compile and about 30 seconds on cached copies of PostStreamer. Installing .Net 4.5.1 fixed it and now it takes 19 seconds on compile and about 1 second after that (what you and Martin were reporting). I can live with those results.
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Possibly Related Threads…
Thread Author Replies Views Last Post
  Windows Desktop Gadget support thread cncb 37 41,458 2017-07-15, 09:43 PM
Last Post: Bobins
  Mobile iNEWA Support Thread UncleJohnsBand 164 83,399 2016-03-19, 05:46 PM
Last Post: UncleJohnsBand
  NEWA Logon not working (to change) themes etc after 3.1.1 jksmurf 11 3,538 2013-11-12, 04:43 AM
Last Post: UncleJohnsBand
  Tablet support billybo8 1 1,569 2013-09-12, 09:45 PM
Last Post: UncleJohnsBand
  NEWA TV Guide Color Change Possible Please? yardern 2 1,771 2012-12-02, 03:01 PM
Last Post: UncleJohnsBand
  Web URL change jaakl 1 1,686 2012-03-13, 02:26 AM
Last Post: UncleJohnsBand
  Change TV Guide Minute Span to server setting? Stereodude 16 4,889 2009-10-12, 07:35 AM
Last Post: sunnybilly
  Cannot enable table view liteswap 4 1,927 2009-08-30, 02:03 PM
Last Post: liteswap
  Request for FLAC support JonRoyle 8 2,773 2009-03-23, 03:08 PM
Last Post: Dave_M
  change in guide.aspx format medic29 1 1,384 2009-03-01, 05:55 AM
Last Post: UncleJohnsBand

  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode