我想向需要身份验证的页面发出Web请求.我该怎么做呢?我找到了一些可能使用Credentials属性的内容,但我不确定如何使用它.
NetworkCredential
为Credentials
属性分配新实例:
webClient.Credentials = new NetworkCredential("Mehrdad", "Password");
基本身份验证示例:
public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword) { string authInfo = userName + ":" + userPassword; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); req.Headers["Authorization"] = "Basic " + authInfo; }
http://blog.kowalczyk.info/article/at3/Forcing-basic-http-authentication-for-HttpWebReq.html