WebRequest.PreAuthenticate 属性

定义

在后代类中重写时,指示是否预先对请求进行身份验证。

public:
 virtual property bool PreAuthenticate { bool get(); void set(bool value); };
public virtual bool PreAuthenticate { get; set; }
member this.PreAuthenticate : bool with get, set
Public Overridable Property PreAuthenticate As Boolean

属性值

true 预身份验证;否则,false

例外

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

示例

以下示例将 PreAuthenticate 属性设置为 true,以便将存储在 Credentials 属性中的 NetworkCredential 与资源请求一起发送到该属性。

// Create a new webrequest to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( url );

// Set 'Preauthenticate' property to true. Credentials will be sent with the request.
myWebRequest->PreAuthenticate = true;

Console::WriteLine( "\nPlease enter your credentials for the requested Url" );
Console::WriteLine( "UserName" );
String^ UserName = Console::ReadLine();
Console::WriteLine( "Password" );
String^ Password = Console::ReadLine();

// Create a New 'NetworkCredential' object.
NetworkCredential^ networkCredential = gcnew NetworkCredential( UserName,Password );

// Associate the 'NetworkCredential' object with the 'WebRequest' object.
myWebRequest->Credentials = networkCredential;

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Create a new webrequest to the mentioned URL.
WebRequest myWebRequest=WebRequest.Create(url);

// Set 'Preauthenticate'  property to true.  Credentials will be sent with the request.
myWebRequest.PreAuthenticate=true;

Console.WriteLine("\nPlease enter your credentials for the requested Url");
Console.WriteLine("UserName");
string UserName=Console.ReadLine();
Console.WriteLine("Password");
string Password=Console.ReadLine();

// Create a New 'NetworkCredential' object.
NetworkCredential networkCredential=new NetworkCredential(UserName,Password);

// Associate the 'NetworkCredential' object with the 'WebRequest' object.
myWebRequest.Credentials=networkCredential;

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();

' Create a new webrequest to the mentioned URL.
Dim myWebRequest As WebRequest = WebRequest.Create(url)

' Set 'Preauthenticate'  property to true.
myWebRequest.PreAuthenticate = True
Console.WriteLine(ControlChars.Cr + "Please enter your credentials for the requested Url")
Console.WriteLine("UserName")
Dim UserName As String = Console.ReadLine()
Console.WriteLine("Password")
Dim Password As String = Console.ReadLine()

' Create a New 'NetworkCredential' object.
Dim networkCredential As New NetworkCredential(UserName, Password)

' Associate the 'NetworkCredential' object with the 'WebRequest' object.
myWebRequest.Credentials = networkCredential

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

注解

谨慎

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

除了第一个请求之外,PreAuthenticate 属性指示是否使用后续请求发送身份验证信息,而无需等待服务器质询。 当 PreAuthenticatefalse时,WebRequest 在发送身份验证信息之前等待身份验证质询。

注意

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

适用于

另请参阅