0

I am trying to use VB.net to connect to a REST API using HTTP Basic Authentication. The authentication succeeds but subsequent requests still yield a 401 error, what am I missing?

    Dim Client As New WebClient

    Client.Credentials = New NetworkCredential("user","password")

    ' Works and authenticates
    MsgBox(Client.DownloadString("http://site/api/login"))

    ' Returns 401
    MsgBox(Client.DownloadString("http://site/api/helloworld"))

I should add, if I go to /api/login and authenticate in a browser, I can then request /api/helloworld correctly - so the error is client side.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Adam Bradbury
  • 93
  • 2
  • 9
  • 1
    The site probably sends a *cookie* after authentication which you are supposed to return on subsequent requests. Have a look at http://stackoverflow.com/q/11118712/87698 – Heinzi Jul 13 '15 at 13:35

1 Answers1

0

Try this:

Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes("user" & ":" & "password"))
Client.Headers(HttpRequestHeader.Authorization) = String.Format("Basic {0}", credentials)
joehanna
  • 1,471
  • 1
  • 11
  • 22