4

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();
2
  • 1
    Depends entirely on how the site that you're requesting from handles authentication. Can you fill in any details? (For most internet sites, you'll have to present a session cookie obtained from a logged in user.) Commented Jan 22, 2012 at 23:59
  • There is login, password and employee ID required while accessing authenticated pages in a normal way. Commented Jan 23, 2012 at 0:13

2 Answers 2

5

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

request.Credentials = new NetworkCredential("username", "password");
Sign up to request clarification or add additional context in comments.

1 Comment

The same works well for Integrated Windows authentication scheme as well if you have domain credentials of a specific user.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.