How do I authenticate a WebClient request? How do I authenticate a WebClient request? asp.net asp.net

How do I authenticate a WebClient request?


You need to give the WebClient object the credentials. Something like this...

 WebClient client = new WebClient(); client.Credentials = new NetworkCredential("username", "password");


What kind of authentication are you using? If it's Forms authentication, then at best, you'll have to find the .ASPXAUTH cookie and pass it in the WebClient request.

At worst, it won't work.


Public Function getWeb(ByRef sURL As String) As String    Dim myWebClient As New System.Net.WebClient()    Try        Dim myCredentialCache As New System.Net.CredentialCache()        Dim myURI As New Uri(sURL)        myCredentialCache.Add(myURI, "ntlm", System.Net.CredentialCache.DefaultNetworkCredentials)        myWebClient.Encoding = System.Text.Encoding.UTF8        myWebClient.Credentials = myCredentialCache        Return myWebClient.DownloadString(myURI)    Catch ex As Exception        Return "Exception " & ex.ToString()    End TryEnd Function