HTTP Hanlder for string return asp.net

孤者浪人 提交于 2019-12-11 16:26:45

问题


I am using vlc plugin to play rtmp links for live streaming. It works fine if place the streaming link in target tag like in below code:

<embed 
type='application/x-vlc-plugin'
pluginspage='http://www.videolan.org'
version='VideoLAN.VLCPlugin.2'
width='800'
height='600'
id='vlc'
loop='yes'
autoplay='yes'
target="rtmp://122.221.75.124:1935/live/myc001">
</embed>

Issue: So my requirement is to hide the rtmp links from viewers by showing html source code. I used Http Handler to return links from Database. The vlc plugin Target tag is:

target="Handler1.ashx?ChannelID=22">

And my Hanlder1.ashx code is:

 public class Handler1 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.QueryString["ChannelID"] == null) return;
        string connStr = Connection_Class.Str_Conn;
        string channelID = context.Request.QueryString["ChannelID"];
        if (channelID == "") { return; }

        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Link FROm TblChannel WHERE ChannelID = @id", conn))
            {
                cmd.Parameters.Add(new SqlParameter("@id", channelID));
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    reader.Read();
                    if (reader.HasRows == true)
                    {
                        string lnk = "";
                        lnk = reader["Link"].ToString();
                        context.Response.Clear();
                        context.Response.Write(lnk);
                        reader.Close();
                        context.Response.End();
                    }
                    else
                    { return; }
                }
            }
        }

The Hanlder1 return the link correctly when I type it in Browser:

http://localhost:54091/handler1.ashx?channelid=22

I used the same approach in vlc Target tage: (target="http://localhost:54091/handler1.ashx?channelid=22") but link does not play. I used break points to verify that the handler works and return with string but vlc doesn't play the stream.


回答1:


I tried 302 redirect and the VLC doesn't react on either. Problem is the vlc is expecting rtmp or rtmpt protocol and you're trying to redirect it on http layer - that wont't work. You'll have to do redirection on rmpt level or create rmpt proxy instead of http handler. If you're going that way take a look at http://www.fluorinefx.com/download.html - seems like great .NET API for the job.

Other way around (I don't know how much control you have over the rmpt server itself and this way presume you does have some control over it) is to hack into handshake phase - i.e. you'll generate link to rmtp directly and as a part of the link you'll pass symetrically encrypted token. - ie. rmpt://whatver?token=secret in the secret part will be timestamp and some random rubish to make it secure; rmpt server will decode token and reject all requests that have old timestamp - that way people can see the link, but it will become unusable after some time.




回答2:


The problem is your handler is returning the url of the stream, not the stream itself which is what VLC is expecting. The easiest way to deal with this is rather than use a handler and point the target at it, is to set the target attribute from asp when you get the page like so:

<embed 
type='application/x-vlc-plugin'
pluginspage='http://www.videolan.org'
version='VideoLAN.VLCPlugin.2'
width='800'
height='600'
id='vlc'
loop='yes'
autoplay='yes'
target="<%= GetStreamLocation() %>" />

And then in the code behind for the page have a function:

protected string GetStreamLocation()
{
    int channelId;
    var success = int.TryParse(Request.QueryString["ChannelID"], out channelId);
    if (!success) throw new Exception("Invalid channel id specified");
    string returnValue;

    using (SqlConnection conn = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Link FROM TblChannel WHERE ChannelID = @id", conn))
        {
            cmd.Parameters.Add(new SqlParameter("@id", channelID));
            conn.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                reader.Read();
                if (reader.HasRows == true)
                {
                    returnValue = reader["Link"].ToString();
                    reader.Close();
                }
            }
        }
    }
    if (string.IsNullOrEmpty(returnValue)) throw new Exception("Error loading link from database");
    return returnValue;
}

This will automatically insert the link for the feed into the target attribute when the page loads.



来源:https://stackoverflow.com/questions/21826415/http-hanlder-for-string-return-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!