次の方法で共有


WebHeaderCollection.GetValues メソッド

ヘッダーに格納されたヘッダー値の配列を取得します。

オーバーロードの一覧

ヘッダーに格納されたヘッダー値の配列を取得します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Overrides Public Function GetValues(String) As String()

[C#] public override string[] GetValues(string);

[C++] public: String* GetValues(String*) __gc[];

[JScript] public override function GetValues(String) : String[];

NameValueCollection から継承されます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Function GetValues(Integer) As String()

[C#] public virtual string[] GetValues(int);

[C++] public: virtual String* GetValues(int) __gc[];

[JScript] public function GetValues(int) : String[];

使用例

[Visual Basic, C#, C++] GetValues メソッドを使用して、 WebHeaderCollection の各ヘッダーの値の配列を取得する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、GetValues のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
'Create a web request for "www.msn.com".
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://www.msn.com"), HttpWebRequest)
'********* the line 23 is causing problem . it is not necessary and can be deleted.
        myHttpWebRequest.Timeout = 1000
'Get the associated response for the above request.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

'Get the headers associated with the response.
Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebResponse.Headers

'The Snippet for 'GetValues(string)' method of 'WebHeaderCollection' class starts here---->
Dim i As Integer
For i = 0 To myWebHeaderCollection.Count - 1
    Dim header As [String] = myWebHeaderCollection.GetKey(i)
    Dim values As [String]() = myWebHeaderCollection.GetValues(header)
    If values.Length > 0 Then
        Console.WriteLine("The values of {0} header are : ", header)
        Dim j As Integer
        For j = 0 To values.Length - 1
            Console.WriteLine(ControlChars.Tab + "{0}", values(j))
        Next j
    Else
        Console.WriteLine("There is no value associated with the header")
    End If
Next i 

[C#] 
// Create a web request for "www.msn.com".
 HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("https://www.msn.com");
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");
}
myHttpWebResponse.Close();

[C++] 
// Create a web request for S"www.msn.com".
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*> (WebRequest::Create(S"https://www.msn.com"));
myHttpWebRequest->Timeout = 1000;
// Get the associated response for the above request.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<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(S"The values of {0} header are : ", header);
      for (int j = 0; j < values->Length; j++)
         Console::WriteLine(S"\t {0}", values->Item[j]);
   } else
      Console::WriteLine(S"There is no value associated with the header");
}
myHttpWebResponse->Close();

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

WebHeaderCollection クラス | WebHeaderCollection メンバ | System.Net 名前空間