Search code examples
c#m3u8

Check m3u8 web url with C#


I'm doing a little function inside a program, and I'll like to check if a m3u8 link is working. However, I'm not able to do it correctly, because some links are not working, but they are returning a status code equals to OK. Here you have my code:

var textBox     = (TextBox)this.FindName("urlToCheck");
var request     = (HttpWebRequest)WebRequest.Create(textBox.Text.Trim());
request.Method  = "HEAD";

try
{
    var response = (HttpWebResponse)request.GetResponse();
    var success  = response.StatusCode == HttpStatusCode.OK;

    if (success) MessageBox.Show("Apparently the link is working");
    else MessageBox.Show("Apparently the link is not working");
}
catch (Exception)
{
    MessageBox.Show("Tthe link is not working");
}

How can I detect if there is real stream inside the working links? I'm not sure how to do it, how to detect a working URL Stream and one that not. The only way now for me, is by using the VLC Player.

Lot of thanks for your help.

Best regards


Solution

  • Finally I fixed it by checking the status code of the link and the contentlength:

    var success = response.StatusCode == HttpStatusCode.OK && response.ContentLength > 0;