WebRequest.Credentials 属性

定义

在后代类中重写时,获取或设置用于使用 Internet 资源对请求进行身份验证的网络凭据。

public:
 virtual property System::Net::ICredentials ^ Credentials { System::Net::ICredentials ^ get(); void set(System::Net::ICredentials ^ value); };
public virtual System.Net.ICredentials Credentials { get; set; }
public virtual System.Net.ICredentials? Credentials { get; set; }
member this.Credentials : System.Net.ICredentials with get, set
Public Overridable Property Credentials As ICredentials

属性值

包含与请求关联的身份验证凭据的 ICredentials。 默认值为 null

例外

如果未在子代类中重写属性,则会尝试获取或设置该属性。

示例

以下示例使用当前用户的默认凭据设置 Credentials 属性。 发出请求时,存储在此属性中的凭据用于验证客户端。 这与将 UseDefaultCredentials 属性设置为 true相同。

// Create a request for the URL.   
WebRequest^ request = WebRequest::Create( "http://www.contoso.com/default.html" );

// If required by the server, set the credentials.
request->Credentials = CredentialCache::DefaultCredentials;

// Get the response.
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
// Create a request for the URL. 		
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
' Create a request for the URL. 		
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

Credentials 属性包含访问 Internet 资源所需的身份验证凭据。

注意

WebRequest 类是 abstract 类。 运行时 WebRequest 实例的实际行为由 WebRequest.Create 方法返回的后代类确定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequest

适用于

另请参阅