Is it possible to use and httphandler to download image, serve to browser and repeat process? RRS feed

  • Question

  • User-1095385363 posted


    I was wondering if it is possible to use an httphandler to download an image, serve that image to the browser and then do it again.

    I currently have access to a url that produces a snapshot image from an ip camera that I would like to continue to pull from.  Essentially making a slide show of the snapshots indefinitely.

    I have already figured out how to download the image and display it.  The next step would to, for a lack of better terms, recursively repeat the process.

    I certainly can do this with Ajax calls from the client but would much rather remove handle it at the server.



    using Newtonsoft.Json.Linq;
    using System;
    using System.Net;
    using System.Web;
    using System.Threading;
    namespace RedLight.App_Code
        class CameraSnapshotHandler : IHttpAsyncHandler
            public bool IsReusable { get { return false; } }
            public CameraSnapshotHandler() { }
            public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
                SnapshotAsynchOperation asynch = new SnapshotAsynchOperation(cb, context, extraData);
                return asynch;
            public void EndProcessRequest(IAsyncResult result) { }
            public void ProcessRequest(HttpContext context)
                throw new InvalidOperationException();
        class SnapshotAsynchOperation : IAsyncResult
            private bool _completed;
            private Object _state;
            private AsyncCallback _callback;
            private HttpContext _context;
            bool IAsyncResult.IsCompleted { get { return _completed; } }
            WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }
            Object IAsyncResult.AsyncState { get { return _state; } }
            bool IAsyncResult.CompletedSynchronously { get { return false; } }
            public SnapshotAsynchOperation(AsyncCallback callback, HttpContext context, Object state)
                _callback = callback;
                _context = context;
                _state = state;
                _completed = false;
            public void StartAsyncWork()
                ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
            private void StartAsyncTask(Object workItemState)
                using (var client = new WebClient())
                    // Get Json data
                    string username;
                    string password;
                    using (var credClient = new WebClient())
                        dynamic stuff = JObject.Parse(credClient.DownloadString(new Uri("http://www.some-url.com/servicestack/equipment-credentials.json?equipmentId=" + _context.Request.QueryString["id"])));
                        username = stuff.Username;
                        password = stuff.Password;
                    // Wait until we have full buffer before displaying
                    _context.Response.BufferOutput = true;
                    // Set content type to match
                    _context.Response.ContentType = "image/png";
                    // Digest Authenticate
                    client.Credentials = new NetworkCredential(username, password);
                    // Download into bit array
                    byte[] content = client.DownloadData("http://some-url/cgi/image.php?type=snapshot");
                    // Output stream to client
                    _context.Response.OutputStream.Write(content, 0, content.Length);
                _completed = true;

    Monday, November 11, 2013 6:58 PM