Aracılığıyla paylaş


WebClient.Headers Özellik

Tanım

İstekle ilişkili üst bilgi adı/değer çiftleri koleksiyonunu alır veya ayarlar.

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

Özellik Değeri

WebHeaderCollection Bu istekle ilişkili üst bilgi adı/değer çiftleri içeren.

Örnekler

Aşağıdaki kod örneği, form verilerinin Headers gönderiye eklendiğini sunucuya bildirmek üzere application/x-www-form-urlencoded, HTTP Content-Type üst bilgisini olarak ayarlamak için koleksiyonu kullanır.

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))

Açıklamalar

özelliği, Headers istekle birlikte gönderen protokol üst bilgilerini WebClient içeren bir WebHeaderCollection örnek içerir.

Bazı ortak üst bilgiler kısıtlı olarak kabul edilir ve sistem tarafından korunur ve bir WebHeaderCollection nesnede ayarlanamaz veya değiştirilemez. Bir nesneyle WebClient ilişkili nesnede WebHeaderCollection bu kısıtlı üst bilgilerden birini ayarlama girişimi, daha sonra isteği göndermeye WebClient çalışırken bir özel durum oluşturur.

Sistem tarafından korunan kısıtlı üst bilgiler şunlardır ancak bunlarla sınırlı değildir:

  • Tarih

  • Host

Ayrıca, nesne WebClient kullanılırken diğer bazı üst bilgiler de kısıtlanır. Bu kısıtlanmış üst bilgiler aşağıdakileri içerir ancak bunlarla sınırlı değildir:

  • Kabul Et

  • Bağlantı

  • İçerik Uzunluğu

  • Expect (değer "100-continue" olarak ayarlandığında

  • If-Modified-Since

  • Aralık

  • Transfer-Encoding

sınıfı, HttpWebRequest yukarıdaki üst bilgilerden bazılarını ayarlamaya yönelik özelliklere sahiptir. Bir uygulamanın bu üst bilgileri ayarlaması önemliyse, HttpWebRequest sınıfı yerine WebRequest sınıfı kullanılmalıdır.

Web sunucuları ve önbellekleri bir Web isteğinde üst bilgileri değiştirebileceği veya ekleyebileceği için üst bilgi değerlerinin değişmeden kalacağını varsaymamalısınız.

Şunlara uygulanır

Ayrıca bkz.