I am making a call to a page on my site using webclient. I’m trying to get the result of the webpage put into a pdf so I am trying to get a string representation of the rendered page. The problem is that the request is not authenticated so all I get is a login screen. I have sent the UseDefaultCredentials property to true but I still get the same result. Below is a portion of my code:
WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; webClient.UseDefaultCredentials = true; return Encoding.UTF8.GetString(webClient.UploadValues(link, "POST",form));
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
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
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 Try End Function
This helped me to call API that was using cookie authentication. I have passed authorization in header like this: