简体   繁体   中英

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();

对于基本身份验证(不确定其他身份验证方案):

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM