uaing HTTP Basic Authentication with VB

10 views
Skip to first unread message

jpdenoyer

unread,
Dec 2, 2008, 9:58:04 PM12/2/08
to Twitter Development Talk
Does anyone know how to use a VB (VB express 2005) program to access a
webpage that requires HTTP Basic Authentication for access?

Below is the code I have so far. It obviously does not work because
there is no authentification info supplied (and I do not know how to
supply the info).

Public Class Form1
Const URL As String = "http://twitter.com/statuses/
friends_timeline.xml?count=1"


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim request As WebRequest = WebRequest.Create(URL)


Dim response As WebResponse = request.GetResponse()

Dim rssStream As Stream = response.GetResponseStream()


Dim rssDoc As XmlDocument = New XmlDocument()
rssDoc.Load(rssStream)

DisplayNode(rssDoc, 0)


End Sub

Private Sub DisplayNode(ByVal node As XmlNode, ByVal depth As
Integer)
' Define the indent level.
Dim Indent As New String(" "c, depth * 4)

' Display the node type.
TextBox1.Text &= (Indent & node.NodeType.ToString() & _
": <" & node.Name & ">")

' Display the node content, if applicable.
If node.Value <> String.Empty Then
Console.WriteLine(Indent & "Value: " & node.Value)
End If

' Display all nested nodes.
Dim Child As XmlNode
For Each Child In node.ChildNodes
DisplayNode(Child, depth + 1)
Next
End Sub
End Class

Matt Sanford

unread,
Dec 2, 2008, 10:16:31 PM12/2/08
to Twitter Development Talk
Hi there,

My VB knowledge is very old and very poor, and my .NET is only
slightly better. Take a look at:

request.PreAuthenticate = True
request.Credentials = new NetworkCredential(UserName, Password)

I could easily be wrong. Take a look at
http://msdn.microsoft.com/en-us/library/system.net.webrequest_members(VS.71).aspx

Thanks;
— Matt Sanford (@mzsanford)

jpdenoyer

unread,
Dec 3, 2008, 7:56:45 AM12/3/08
to Twitter Development Talk
Thank you Matt. Those two lines worked perfectly.

J.P.
Reply all
Reply to author
Forward
0 new messages