WebClient.Headers Właściwość

Definicja

Pobiera lub ustawia kolekcję par nazwy/wartości nagłówka skojarzonych z żądaniem.

public:
 property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public Property Headers As WebHeaderCollection

Wartość właściwości

Pary WebHeaderCollection nazwy/wartości nagłówka skojarzone z tym żądaniem.

Przykłady

Poniższy przykład kodu używa Headers kolekcji do ustawienia nagłówka HTTP Content-Type w celu application/x-www-form-urlencoded, powiadomienia serwera, że dane formularza są dołączone do wpisu.

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, http://www.contoso.com}: " );
uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

// Displays the headers in the request
Console::Write( "Resulting Request Headers: ");
Console::WriteLine(myWebClient->Headers);

// Apply ASCII Encoding to obtain the String^ as a Byte array.
array<Byte>^ byteArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
// Upload the input String* using the HTTP 1.0 POST method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, "POST", byteArray );
// Decode and display the response.
Console::WriteLine( "\nResponse received was {0}",
   Encoding::ASCII->GetString( responseArray ) );
   string uriString;
       Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
       uriString = Console.ReadLine();

       // Create a new WebClient instance.
       WebClient myWebClient = new WebClient();
       Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
       string postData = Console.ReadLine();
       myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // Apply ASCII Encoding to obtain the string as a byte array.

       byte[] byteArray = Encoding.ASCII.GetBytes(postData);
       Console.WriteLine("Uploading to {0} ...",  uriString);						
       // Upload the input string using the HTTP 1.0 POST method.
       byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
       
       // Decode and display the response.
       Console.WriteLine("\nResponse received was {0}",
       Encoding.ASCII.GetString(responseArray));
                 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' Display the headers in the request
Console.Write("Resulting Request Headers: ")
Console.Writeline(myWebClient.Headers.ToString())

' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

Uwagi

Właściwość Headers zawiera WebHeaderCollection wystąpienie zawierające nagłówki protokołu wysyłane WebClient za pomocą żądania.

Niektóre typowe nagłówki są uważane za ograniczone i są chronione przez system i nie można ich ustawić ani zmienić w WebHeaderCollection obiekcie. Każda próba ustawienia jednego z tych ograniczonych nagłówków w WebHeaderCollection obiekcie skojarzonym z obiektem WebClient zgłosi wyjątek później podczas próby wysłania WebClient żądania.

Ograniczone nagłówki chronione przez system obejmują, ale nie są ograniczone do następujących:

  • Date

  • Host

Ponadto niektóre inne nagłówki są również ograniczone podczas korzystania z WebClient obiektu. Te ograniczone nagłówki obejmują, ale nie są ograniczone do następujących:

  • Zaakceptuj

  • Połączenie

  • Długość zawartości

  • Oczekiwano (gdy wartość jest ustawiona na "100-continue"

  • If-Modified-Since

  • Zakres

  • Transfer-Encoding

Klasa HttpWebRequest ma właściwości ustawiania niektórych powyższych nagłówków. Jeśli ważne jest, aby aplikacja ustawiła te nagłówki, HttpWebRequest należy użyć klasy zamiast WebRequest klasy.

Nie należy zakładać, że wartości nagłówka pozostaną niezmienione, ponieważ serwery sieci Web i pamięci podręczne mogą zmieniać lub dodawać nagłówki do żądania sieci Web.

Dotyczy

Zobacz też