Cannot subtract data stream, always empty

Trying to download a video (any) from the service Sibnet, I tested getting a link to a video file, and I successfully get a normal Url, but at the same time, if I make a request to read the data stream, nothing happens, and the data stream is always empty, although the response code is 200.

In general, to get a link to a file, you need to do the following: get a link to the file mpd, rename mpd to mp4, and make a request. If you do all this with browser, it turns out that when you request Url, a redirect is made to one of the servers sibnet where the file is located(well, more precisely, access from there will be allowed, the link is always different given). In my case, when requesting such a Url redirection does not occur, and HttpClient just reads the response of the headers, no more.

The code below is functional, but cannot read the content.

What can be done to make the content readable, as well as to make it work redirection?

public class Program
{
    public static void Main(string[] args)
    {
        CookieCollection cookieCollection = new CookieCollection();
        CookieContainer container = new CookieContainer();
        container.Add(cookieCollection);

        HttpClientHandler clientHandler = new HttpClientHandler
        {
            AllowAutoRedirect = true,
            UseCookies = true
        };

        HttpClient client = new HttpClient(clientHandler, true)
        {
            BaseAddress = new Uri("https://video.sibnet.ru/")
        };

        string result =
            client.GetStringAsync
            (
                new Uri(
                    "/shell.php?videoid=3490241",
                    UriKind.Relative)
            ).GetAwaiter().GetResult();

        Regex regex = new Regex(@"\[\{src\:\s?\""(?<FilePath>[^\""]+)\""");
        Match match = regex.Match(result);
        Console.WriteLine(match.Groups["FilePath"].Value.Replace("mpd", "mp4").Replace("m3u8", "mp4"));

        client.DefaultRequestHeaders.Referrer = new Uri(client.BaseAddress.OriginalString + "/shell.php?videoid=3490241");
        client.DefaultRequestHeaders.Add("Origin", "https://video.sibnet.ru");

        HttpResponseMessage message = client
            .GetAsync(new Uri(match.Groups["FilePath"].Value.Replace("mpd", "mp4").Replace("m3u8", "mp4"),
                UriKind.Relative)).GetAwaiter().GetResult();

        message = message.EnsureSuccessStatusCode();

        Stream fileData = message.Content.ReadAsStreamAsync().GetAwaiter().GetResult();

        FileStream file = File.Create("sibnet.mp4");
        byte[] data = new byte[8388608]; // 8 Mb 1024*1024*8
        int offset = 0;
        while ((offset = fileData.Read(data, 0, data.Length)) != 0) file.Write(data, offset, data.Length);

        file.Close();
        fileData.Close();
        client.Dispose();
    }
Author: mik.ov, 2018-12-24

1 answers

It turns out that the problem was different, that sibnet would give a link to the full file, you need to send POST a request with the content buffer_method: full, then in the source code you can find the full link to the file, but you should not forget that sibnet checks 2 headers, these are Origin, and Referer. In general, the code below can be safely used as for downloading (any) video from sibnet.

But I still have a question, how can I use the received Stream to write for example melon to a file or for example to a local socket server (streaming on a local network)?

public class Program
{
    public static async Task Main(string[] args)
    {
        Uri loadUri = new Uri("/shell.php?videoid=3490241", UriKind.Relative);
        Regex regex = new Regex(@"src\:\s?\""(?<FilePath>[^\""]+)\""");

        CookieCollection cookieCollection = new CookieCollection();
        CookieContainer container = new CookieContainer();
        container.Add(cookieCollection);

        HttpClientHandler clientHandler = new HttpClientHandler
        {
            AllowAutoRedirect = true,
            UseCookies = true
        };

        HttpClient client = new HttpClient(clientHandler, true)
        {
            BaseAddress = new Uri("https://video.sibnet.ru/")
        };

        HttpResponseMessage result = await client.PostAsync(loadUri, new FormUrlEncodedContent(new []{new KeyValuePair<string, string>("buffer_method", "full")}));

        Match match = regex.Match(await result.Content.ReadAsStringAsync());
        Console.WriteLine(match.Groups["FilePath"].Value);

        client.DefaultRequestHeaders.Referrer = new Uri(client.BaseAddress, loadUri);
        client.DefaultRequestHeaders.Add("Origin", client.BaseAddress.OriginalString);

        Stream message = await client.GetStreamAsync(new Uri(match.Groups["FilePath"].Value, UriKind.Relative));

        FileStream file = File.Create("sibnet.mp4");
        await message.CopyToAsync(file, 8388608);

        message.Dispose();
        file.Dispose();
        client.Dispose();
    }
}
 0
Author: mik.ov, 2018-12-24 15:00:06