LicenseAcquirer.SetLicenseResponse Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Sets the license response by passing the SOAP body from the HTTP response to the license challenge.
Assembly: System.Windows (in System.Windows.dll)
Parameters
- licenseResponse
- Type: System.IO.Stream
The SOAP body from the HTTP response to the license challenge. The SOAP body is passed in as a stream and contains the entire SOAP response.
| Exception | Condition |
|---|---|
| ArgumentNullException | The licenseResponse is Nothing. |
| InvalidOperationException | The specified licenseResponse is invalid. |
If the LicenseAcquirer is created on demand (from the media element) and if this method is called with Nothing, the MediaElement transitions from the AcquiringLicense state to the error state and raises the MediaFailed event corresponding to that.
If the LicenseAcquirer is created on demand (from the media element) and if you are handling license acquisition on your own by overriding OnAcquireLicense, you are expected to call this method before the MediaElement leaves the AcquiringLicense state.
The LicenseAcquirer class is used by the MediaElement to handle acquiring licenses for DRM encrypted content from a PlayReady License Server. You can subclass the LicenseAcquirer class and add custom logic like adding your own custom authentication scheme to the license request.
The following example shows how to override the LicenseAcquirer class (named "ManualLicenseAcquirer") and have a MediaElement use it to acquire the license.
<StackPanel x:Name="LayoutRoot" Background="Gray" Orientation="Vertical"> <MediaElement x:Name="myME" Height="100"/> </StackPanel>
Public Class Page Inherits UserControl Public Sub New() MyBase.New InitializeComponent AddHandler Loaded, AddressOf Me.Page_Loaded End Sub Private Sub Page_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) ' Test a full fledged manual acquirer ' Set the LicenseAcquirer of the MediaElement to the custom License Acquirer ' defined in this sample. myME.LicenseAcquirer = New ManualLicenseAcquirer(myME.Name) ' Set the License URI to proper License Server address. myME.LicenseAcquirer.LicenseServerUriOverride = New Uri("http://contoso.com/myLicenseServer.asmx", UriKind.Absolute) AddHandler myME.MediaFailed, AddressOf Me.myME_MediaFailed ' Set the source of the MediaElement to the URL of the media encrypted with WMDRM. myME.Source = New Uri("http:, contoso.com/wmdrm_url.wmv", UriKind.Absolute);) End Sub Private Sub myME_MediaFailed(ByVal sender As Object, ByVal e As ExceptionRoutedEventArgs) Dim errorMessage As String = "" If e.ErrorException.ToString.Contains(" 6001 ") Then errorMessage = "The individualization component software failed to" & _ " download to the user's computer. This error would" & _ " come up when the MediaElement is in the Individualizing" & _ " MediaElementState. One possible reason for this error is" & _ " that Windows Phone cannot connect the Microsoft" & _ " Individualization Server." ElseIf e.ErrorException.ToString.Contains(" 6004 ") Then errorMessage = "The installation of Windows Phone on the device is" & _ " out of date and needs to be updated." Else errorMessage = "MediaFailed: " & _ + e.ErrorException.Message + "." End If System.Windows.Browser.HtmlPage.Window.Alert(errorMessage) End Sub ' makes license request explicitly Public Class ManualLicenseAcquirer Inherits LicenseAcquirer Private challengeString As String Private _mediaElementName As String Public Sub New(ByVal mediaElementName As String) MyBase.New _mediaElementName = mediaElementName End Sub ' The default implementation of OnAcquireLicense calls into the MediaElement to acquire a ' license. It is called when the Media pipeline is building a topology and will be raised ' before MediaOpened is raised. Protected Overrides Sub OnAcquireLicense(ByVal licenseChallenge As System.IO.Stream, ByVal licenseServerUri As Uri) Dim sr As StreamReader = New StreamReader(licenseChallenge) challengeString = sr.ReadToEnd ' Need to resolve the URI for the License Server -- make sure it is correct ' and store that correct URI as resolvedLicenseServerUri. Dim resolvedLicenseServerUri As Uri If (LicenseServerUriOverride Is Nothing) Then resolvedLicenseServerUri = licenseServerUri Else resolvedLicenseServerUri = LicenseServerUriOverride End If ' Make a HttpWebRequest to the License Server. Dim request As HttpWebRequest = CType(WebRequest.Create(resolvedLicenseServerUri),HttpWebRequest) request.Method = "POST" ' Set ContentType through property request.ContentType = "application/xml" ' ADD REQUIRED HEADERS. ' The headers below are necessary so that error handling and redirects are handled ' properly by Windows Phone. request.Headers("msprdrm_server_redirect_compat") = "false" request.Headers("msprdrm_server_exception_compat") = "false" ' Initiate getting request stream Dim asyncResult As IAsyncResult = request.BeginGetRequestStream(New AsyncCallback(RequestStreamCallback), request) End Sub ' This method is called when the asynchronous operation completes. Private Sub RequestStreamCallback(ByVal ar As IAsyncResult) Dim request As HttpWebRequest = CType(ar.AsyncState,HttpWebRequest) ' populate request stream request.ContentType = "text/xml" Dim requestStream As Stream = request.EndGetRequestStream(ar) Dim streamWriter As StreamWriter = New StreamWriter(requestStream, System.Text.Encoding.UTF8) streamWriter.Write(challengeString) streamWriter.Close ' Make async call for response request.BeginGetResponse(New AsyncCallback(ResponseCallback), request) End Sub Private Sub ResponseCallback(ByVal ar As IAsyncResult) Dim request As HttpWebRequest = CType(ar.AsyncState,HttpWebRequest) Dim response As WebResponse = request.EndGetResponse(ar) SetLicenseResponse(response.GetResponseStream) End Sub End Class End Class