Rusty Nail
Rusty Nail

Reputation: 2710

CefSharp get Header Response for .m3 file

I am looking for video files via the *.m3u4 or 8 extension, but I never get the extension?

enter image description here

Func<NameValueCollection, bool> headersProcessingFunc = new Func<NameValueCollection, bool>(ProcessHeaders);

chromiumWebBrowser1.RequestHandler = new HeadersProcessingRequestHandler(headersProcessingFunc);

private static bool ProcessHeaders(NameValueCollection headers)
{

foreach(string header in headers)
if (headers[header].Contains(".m3"))
MessageBox.Show(header + "\r\n" + headers[header]);
return true;
}

I do get other files, "content" and so on:

alt-svc
cf-cache-status
cf-ray
content-encoding
content-type
date
nel
report-to
server
vary
x-frame-options

I know this might be somewhat controversial, this post, but hey, its a Tech Question, its valid, and people deserve to know about this sort of thing, and to be able to work with it! I realise you need CefSharp to have Codecs enabled: 1 and 2 and 3

Of course, the point is to be able to use ffmpeg to get the Link and Download the Video or Audio.

Upvotes: 2

Views: 344

Answers (1)

ricardkelly
ricardkelly

Reputation: 2288

You want to hook the event where a response to an interesting request comes back. First define the handler to look at each request and decide if its response should be streamed to your custom handler:

    public class MyRequestHandler : CefSharp.Handler.RequestHandler
    {
        protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
        {
            if (request.Url.EndsWith(".m3u4"))
            {
                return new MyResourceRequestHandler();
            }
            return null;
        }
    }

Then attach to your browser:

chromiumWebBrowser1.RequestHandler = new MyRequestHandler();

Your might want a more complicated set of rules to match, but if you can identify your required resource to be intercepted and downloaded, MyResourceRequestHandler will just be called for those.

The code for the custom resource request handler then gets called for interesting responses. If you want the data streamed to you, add a filter and then grab the stream once the response completes.

    public class MyResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
    {
        private readonly System.IO.MemoryStream responseData = new System.IO.MemoryStream();

        protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
        {
            return new CefSharp.ResponseFilter.StreamResponseFilter(responseData);
        }
        
        protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
        {
            var bytes = responseData.ToArray();
            Console.WriteLine("Got {0} for {1}", responseData.Length, request.Url);
        }
    }

Upvotes: 1

Related Questions