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 属性设置为 trueNetworkCredential 以便将存储在 属性中的 Credentials 随资源请求一起发送到 。

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

注解

除了第一个请求之外, PreAuthenticate 属性指示是否在不等待服务器质询的情况下发送后续请求的身份验证信息。 当 为 falsePreAuthenticate,将WebRequest等待身份验证质询,然后再发送身份验证信息。

注意

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

适用于

另请参阅