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.Create 傳回的 WebRequest 子系類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱 子系類別的檔,例如 HttpWebRequestFileWebRequest

適用於

另請參閱