WebHeaderCollection.AllKeys 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得集合中的所有標頭名稱 (索引鍵)。
public:
property cli::array <System::String ^> ^ AllKeys { cli::array <System::String ^> ^ get(); };
public:
virtual property cli::array <System::String ^> ^ AllKeys { cli::array <System::String ^> ^ get(); };
public string[] AllKeys { get; }
public override string[] AllKeys { get; }
member this.AllKeys : string[]
Public ReadOnly Property AllKeys As String()
Public Overrides ReadOnly Property AllKeys As String()
屬性值
String[]
String 類型的陣列,包含 Web 要求中的所有標頭名稱。
範例
下列程式代碼範例會 AllKeys 使用 屬性來取得標頭名稱 WebHeaderCollection。
if (args.Length == 0)
{
Console.WriteLine("must specify a URL!");
return;
}
string server = args[0];
// Create the web request
HttpWebRequest myHttpWebRequest =
(HttpWebRequest) WebRequest.Create(server);
myHttpWebRequest.Timeout = 1000;
// Get the associated response for the above request.
HttpWebResponse myHttpWebResponse =
(HttpWebResponse) myHttpWebRequest.GetResponse();
// Get the headers associated with the response.
WebHeaderCollection myWebHeaderCollection =
myHttpWebResponse.Headers;
for(int i = 0; i < myWebHeaderCollection.Count; i++)
{
String header = myWebHeaderCollection.GetKey(i);
String[] values =
myWebHeaderCollection.GetValues(header);
if(values.Length > 0)
{
Console.WriteLine("The values of {0} header are : "
, header);
for(int j = 0; j < values.Length; j++)
Console.WriteLine("\t{0}", values[j]);
}
else
Console.WriteLine("There is no value associated" +
"with the header");
}
Console.WriteLine("");
// Get the headers again, using new properties (Keys,
// AllKeys, Clear) and methods (Get and GetKey)
string[] headers = myWebHeaderCollection.AllKeys;
// enumerate through the header collection.
foreach (string s in headers)
{
Console.WriteLine("Header {0}, value {1}",
s,
myWebHeaderCollection.Get(s) );
}
Console.WriteLine("");
// show the use of Get(Int32) and GetValue(Int32)
if (myWebHeaderCollection.Count > 0)
{
// get the name and value of the first header
int index=0;
Console.WriteLine("Header {0}: name {1}, value {2}",
index,
myWebHeaderCollection.GetKey(index),
myWebHeaderCollection.Get(index));
}
myWebHeaderCollection.Clear();
myHttpWebResponse.Close();