D
D
Daeamon2016-12-22 21:07:55
ASP.NET
Daeamon, 2016-12-22 21:07:55

How to proxy an endless stream?

Hello, there is a task to proxy an audio stream. It was possible to send data to the outgoing stream, but firstly, they were not perceived by Media Player Classic as an audio stream, and secondly, I don’t know how to read and write it endlessly until the user closes the connection page (by closing the player).
Please help me solve the problem) Here's something already thrown

using System;
using System.IO;
using System.Net;
using System.Web;

namespace WebApplication1
{
    public class proxy : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpResponse response = context.Response;
            /*
            // Check for query string
            string uri = Uri.UnescapeDataString(context.Request.QueryString.ToString());
            if (string.IsNullOrWhiteSpace(uri))
            {
                response.StatusCode = 403;
                response.End();
                return;
            }

            // Filter requests
            if (!uri.ToLowerInvariant().Contains("wikimedia.org"))
            {
                response.StatusCode = 403;
                response.End();
                return;
            }
            */
            string uri = "http://111.223.51.8:8600/;?icy=http";
            // Create web request
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            //WebRequest webRequest = WebRequest.Create(new Uri(uri));
            webRequest.Method = "GET";
            webRequest.ContentType = "audio/mpeg";

            // Send the request to the server
            
            WebResponse serverResponse = null;
            try
            {
                serverResponse = webRequest.GetResponse();
            }
            catch (WebException webExc)
            {
                response.StatusCode = 500;
                response.StatusDescription = webExc.Status.ToString();
                response.Write(webExc.Response);
                response.End();
                return;
            }

            // Exit if invalid response
            if (serverResponse == null)
            {
                response.End();
                return;
            }
            // Configure reponse
            //response.ContentType = serverResponse.ContentType;
            response.ContentType = "audio/mpeg";

            var headers = serverResponse.Headers;
            for(int i = 0; i < headers.Count; ++i)
            {
                string header = headers.GetKey(i);
                foreach (string value in headers.GetValues(i))
                {
                    response.AddHeader(header, value);
                }
            }
            //Stream stream = serverResponse.GetResponseStream();


            using (Stream answer = serverResponse.GetResponseStream())
            {

                byte[] buffer = new byte[128];
                for (int read = answer.Read(buffer, 0, buffer.Length); read > 0; read = answer.Read(buffer, 0, buffer.Length))
                {
                    response.OutputStream.Write(buffer, 0, read);
                }
            }
        }
        public bool IsReusable
        {
            get { return false; }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question