C
Chris Becker
I am trying to write a little app that will parse my Tivo's Now Playing
xml doc using ASP.NET (or winforms, i just want something to work at
this point).
Below is the code that I am using. What happens is that I get 401:
Unauthorized when I call req.GetResponse(). But using the same url,
username, password and accepting the cert manually in IE or firefox, I
am able to access the url.
Any insight as to what is happening would be greatly appreciated. I am
thinking that this is a username/password problem because of the 401:
Unauthorized response, but maybe it is a certificate problem? I
haven't found too many examples out there on handling ssl
request/responses using HttpWebRequest or WebClient.
Imports System.xml
Imports System.Net
Imports System.Security.Cryptography.X509Certificates
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Supposedly, the following should accept all SSL certificates.
'Might not be safe and secure, but I wanted to get a working copy
first
ServicePointManager.CertificatePolicy = New
AcceptAllCertificatePolicy()
Const IP As String = "192.168.1.100"
Dim url As String = "https://" + IP +
"/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes"
Dim creds As New CredentialCache
'BEGIN QUESTION: Is the following URI correct?
creds.Add(New Uri("https://" + IP + "/"), "Basic", New
NetworkCredential("tivo", "MyMAKNumberRemoved"))
'END QUESTION
Dim req As HttpWebRequest
req = HttpWebRequest.Create(url)
req.ConnectionGroupName = Guid.NewGuid.ToString 'Added this
just to make sure that my various tests weren't getting cached
req.KeepAlive = False
'BEGIN QUESTION: I have tried the following together and both without
the other
req.Headers.Add("Authorization", "Basic " +
Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("tivo:MyMAKNumberRemoved")))
req.Credentials = creds
'END QUESTION
Dim answer As HttpWebResponse
'c Here's where I get the 401:Unauthorized error
answer = req.GetResponse()
Dim reader As New XmlTextReader(url)
Dim xmltext As New System.Text.StringBuilder
Dim doc As New XmlDocument
doc.Load(reader)
Response.Clear()
Response.Write(doc.InnerText)
End Sub
End Class
'Accept all SSL certificates.
Friend Class AcceptAllCertificatePolicy
Implements ICertificatePolicy
Public Function CheckValidationResult(ByVal srvPoint As
ServicePoint, _
ByVal cert As X509Certificate, ByVal request As WebRequest, ByVal
problem As Integer) _
As Boolean Implements ICertificatePolicy.CheckValidationResult
Return True
End Function
End Class
xml doc using ASP.NET (or winforms, i just want something to work at
this point).
Below is the code that I am using. What happens is that I get 401:
Unauthorized when I call req.GetResponse(). But using the same url,
username, password and accepting the cert manually in IE or firefox, I
am able to access the url.
Any insight as to what is happening would be greatly appreciated. I am
thinking that this is a username/password problem because of the 401:
Unauthorized response, but maybe it is a certificate problem? I
haven't found too many examples out there on handling ssl
request/responses using HttpWebRequest or WebClient.
Imports System.xml
Imports System.Net
Imports System.Security.Cryptography.X509Certificates
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Supposedly, the following should accept all SSL certificates.
'Might not be safe and secure, but I wanted to get a working copy
first
ServicePointManager.CertificatePolicy = New
AcceptAllCertificatePolicy()
Const IP As String = "192.168.1.100"
Dim url As String = "https://" + IP +
"/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes"
Dim creds As New CredentialCache
'BEGIN QUESTION: Is the following URI correct?
creds.Add(New Uri("https://" + IP + "/"), "Basic", New
NetworkCredential("tivo", "MyMAKNumberRemoved"))
'END QUESTION
Dim req As HttpWebRequest
req = HttpWebRequest.Create(url)
req.ConnectionGroupName = Guid.NewGuid.ToString 'Added this
just to make sure that my various tests weren't getting cached
req.KeepAlive = False
'BEGIN QUESTION: I have tried the following together and both without
the other
req.Headers.Add("Authorization", "Basic " +
Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("tivo:MyMAKNumberRemoved")))
req.Credentials = creds
'END QUESTION
Dim answer As HttpWebResponse
'c Here's where I get the 401:Unauthorized error
answer = req.GetResponse()
Dim reader As New XmlTextReader(url)
Dim xmltext As New System.Text.StringBuilder
Dim doc As New XmlDocument
doc.Load(reader)
Response.Clear()
Response.Write(doc.InnerText)
End Sub
End Class
'Accept all SSL certificates.
Friend Class AcceptAllCertificatePolicy
Implements ICertificatePolicy
Public Function CheckValidationResult(ByVal srvPoint As
ServicePoint, _
ByVal cert As X509Certificate, ByVal request As WebRequest, ByVal
problem As Integer) _
As Boolean Implements ICertificatePolicy.CheckValidationResult
Return True
End Function
End Class