简体   繁体   English

C# - 使用HttpWebRequest传递身份验证凭据

[英]C# - passing authentication credentials with HttpWebRequest

I am working on a tool that parses html source of given urls. 我正在研究一种解析给定网址的html源代码的工具。 Some of them are password protected. 其中一些是密码保护。

Here is my question: How can I pass authentication credentials with the HttpWebRequest? 这是我的问题:如何通过HttpWebRequest传递身份验证凭据? Does it require setting up a cookie? 是否需要设置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. 对于页面上下文中有效cookie可用的表单身份验证,您可以使用此答案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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