Jump to content

Code - Example "http Request Header" and "POST Request Class"


chef

Recommended Posts

Code Examples are found throughout GitHub, however here is a .Net Version of a Media Browser Http Request Header.

If you are a new coder like me then you will thank me. Some of this code is a Visual Basic conversion found in the Media Browser Repository on GitHub. 

 

This POST Class will send a command to the Server, it doesn't recieve Information, although you could recieve information back for Error Reporting and Logging purposes.

 

I will save you countless hours of reading code by posting what currently works.

 

Make this class in your application:


Imports System.Text
Imports Core.Neurosphere.User
Imports System.Net
Imports System.IO



Public Class MediaBrowserHttpClient
    Public Shared Sub POST(url As String)
        Try
            Dim encoding As UTF8Encoding = New UTF8Encoding
            Dim ByteData As Byte() = encoding.GetBytes(url)
            Dim header As String =
                    String.Format(
                        "MediaBrowser UserId=""{0}"", Client=""{1}"", Device=""{2}"""", DeviceId=""{3}"", Version=""{4}""",
                        "{YOUR CURRENT USER ID HERE}", "{YOUR CLIENT NAME HERE}", 
                        "{YOUR DEVICE NAME HERE}", "{YOUR DEVICE ID HERE}",
                        "{YOUR APPLICATION VERSION HERE})
            Dim Request As HttpWebRequest = DirectCast(WebRequest.Create(url), 
                                                       HttpWebRequest)
            With Request
                .Method = "POST"
                .Headers("Authorization") = (header)
                .ContentType = "application/x-www-form-urlencoded"
                .ContentLength = ByteData.Length
            End With

            Dim postRequestStream As Stream = Request.GetRequestStream()
            With postRequestStream
                .Write(ByteData, 0, ByteData.Length)
                .Close()
            End With
        Catch Ex As Exception
        End Try
    End Sub
End Class

I made it Shared so you can simply call it by name throughout your Application!

 

Find a Url from the SwaggerUI like "Pause" to pause you media Playback.

 

Example:

Try
    ' :: Request that the Server/Client Application Play the Current Item
    Dim UrlDataRequest As String = "http://" &
                                   "{THE SERVER ADDRESS HERE}" & ":" &
                                   "{THE PORT NUMBER HERE}" &
                                   "/mediabrowser/Sessions/" &
                                   "{THE SESSION ID HERE}" &
                                   "/Playing/pause"

    MediaBrowserHttpClient.POST(UrlDataRequest)
Catch ex As Exception
End Try

It took me forever to figure it out, because I'm new. Hope it saves some trouble for others who are new, and want to wrap their own client interface.

 

In the future there will be apparently other Security information added to the header, but this works now.

 

Save yourself a weekend of reading.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...