Javad Rajabzade Javad Rajabzade - 13 days ago 5
Vb.net Question

Specifying username/password in StreamReader

I'm trying to auto stream a text file to a

TextBox
with the user and pass being specified in the
StreamReader
.

This is my code:

Dim address As String = "http://www.domaintest.com/test/file.txt"
Dim webclient As NetworkCredential = "user:pass"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
TextBox1.Text = reader.ReadToEnd

Answer

Your original example actually returned the file without specifying credentials. But if you want to specify credentials, you can do this

Dim address As String = "http://www.domaintest.com/test/file.txt"
Dim credentials As New NetworkCredential("user", "pass")
Using client As New WebClient()
    client.Credentials = credentials
    Using reader As New StreamReader(client.OpenRead(address))
        TextBox1.Text = reader.ReadToEnd
    End Using
End Using