Luke G
Luke G

Reputation: 1747

C# - passing authentication credentials with HttpWebRequest

I am working on a tool that parses html source of given urls. Some of them are password protected.

Here is my question: How can I pass authentication credentials with the HttpWebRequest? Does it require setting up a cookie? These are new grounds to me, thus examples would be very helpful.

In summary, I use the following for requests that not require an authentication.

...
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(HttpUtility.UrlDecode(<URL   STRING>));
...
HttpWebResponse response =(HttpWebResponse)request.GetResponse();

Upvotes: 4

Views: 14526

Answers (2)

Stuart
Stuart

Reputation: 669

For forms authentication where a valid cookie is available in the page Context, you can use this answer.

https://stackoverflow.com/a/1589723/253131

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292695

For Basic authentication (not sure about other authentication schemes):

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

Upvotes: 5

Related Questions