Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I authenticate a WebClient request?

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));
like image 647
smartdirt Avatar asked Dec 10 '09 20:12

smartdirt


People also ask

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.


2 Answers

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

 WebClient client = new WebClient();
 client.Credentials = new NetworkCredential("username", "password");
like image 78
Ryan Alford Avatar answered Oct 14 '22 08:10

Ryan Alford


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.

like image 29
John Saunders Avatar answered Oct 14 '22 08:10

John Saunders