Freigeben über


WebClient.Headers-Eigenschaft

Ruft eine Auflistung der Name-Wert-Paare für Header auf, die der Anforderung zugeordnet sind, oder legt diese fest.

Namespace: System.Net
Assembly: System (in system.dll)

Syntax

'Declaration
Public Property Headers As WebHeaderCollection
'Usage
Dim instance As WebClient
Dim value As WebHeaderCollection

value = instance.Headers

instance.Headers = value
public WebHeaderCollection Headers { get; set; }
public:
property WebHeaderCollection^ Headers {
    WebHeaderCollection^ get ();
    void set (WebHeaderCollection^ value);
}
/** @property */
public WebHeaderCollection get_Headers ()

/** @property */
public void set_Headers (WebHeaderCollection value)
public function get Headers () : WebHeaderCollection

public function set Headers (value : WebHeaderCollection)

Eigenschaftenwert

Eine WebHeaderCollection mit den dieser Anforderung zugeordneten Name-Wert-Paaren für Header.

Hinweise

Die Headers-Eigenschaft enthält eine WebHeaderCollection-Instanz mit Headerinformationen, die der WebClient mit der Anforderung sendet. Dies ist eine uneingeschränkte Headerauflistung. Das Festlegen von durch WebRequest-Nachkommen, z. B. HttpWebRequest, eingeschränkten Headern ist erlaubt.

Beispiel

Im folgenden Codebeispiel wird der HTTP-Content-Type-Header mit der Headers-Auflistung auf application/x-www-form-urlencoded, festgelegt, um den Server darüber zu benachrichtigen, dass Formulardaten an die Bereitstellung angefügt werden.

Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, https://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")
' 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))
string uriString;
Console.Write("\nPlease enter the URI to post data to {for example, https://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");
// 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));
String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, https://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" );
// 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, https://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.get_Headers().Add("Content-Type",
    "application/x-www-form-urlencoded");
// Apply ASCII Encoding to obtain the string as a byte array.
ubyte byteArray[] = Encoding.get_ASCII().GetBytes(postData);
Console.WriteLine("Uploading to {0} ...", uriString);
// Upload the input string using the HTTP 1.0 POST method.
ubyte responseArray[] = myWebClient.UploadData(uriString, "POST",
    byteArray);
// Decode and display the response.
Console.WriteLine("\nResponse received was {0}", Encoding.
    get_ASCII().GetString(responseArray));

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

WebClient-Klasse
WebClient-Member
System.Net-Namespace
WebRequest.Headers
HttpWebRequest.Headers-Eigenschaft