HttpWebRequest.Headers Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan kumpulan pasangan nama/nilai yang membentuk header HTTP.
public:
virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public override System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public Overrides Property Headers As WebHeaderCollection
Nilai Properti
WebHeaderCollection yang berisi pasangan nama/nilai yang membentuk header untuk permintaan HTTP.
Pengecualian
Permintaan telah dimulai dengan memanggil GetRequestStream()metode , , BeginGetRequestStream(AsyncCallback, Object)GetResponse(), atau BeginGetResponse(AsyncCallback, Object) .
Contoh
Contoh kode berikut menggunakan Headers properti untuk mencetak pasangan nama/nilai header HTTP ke konsol.
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.contoso.com" ) );
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
Console::WriteLine( "\nThe HttpHeaders are \n\n\tName\t\tValue\n {0}", myHttpWebRequest->Headers );
// Print the HTML contents of the page to the console.
Stream^ streamResponse = myHttpWebResponse->GetResponseStream();
StreamReader^ streamRead = gcnew StreamReader( streamResponse );
array<Char>^ readBuff = gcnew array<Char>(256);
int count = streamRead->Read( readBuff, 0, 256 );
Console::WriteLine( "\nThe HTML contents of page the are : \n\n " );
while ( count > 0 )
{
String^ outputData = gcnew String( readBuff,0,count );
Console::Write( outputData );
count = streamRead->Read( readBuff, 0, 256 );
}
streamResponse->Close();
streamRead->Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse->Close();
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",myHttpWebRequest.Headers);
// Print the HTML contents of the page to the console.
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe HTML contents of page the are : \n\n ");
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
Console.Write(outputData);
count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse.Close();
' Create a new 'HttpWebRequest' Object to the mentioned URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Console.WriteLine(ControlChars.Cr + "The HttpHeaders are " + ControlChars.Cr + ControlChars.Cr + ControlChars.Tab + "Name" + ControlChars.Tab + ControlChars.Tab + "Value" + ControlChars.Cr + "{0}", myHttpWebRequest.Headers)
' Print the HTML contents of the page to the console.
Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim readBuff(256) As [Char]
Dim count As Integer = streamRead.Read(readBuff, 0, 256)
Console.WriteLine(ControlChars.Cr + "The HTML contents of page the are : " + ControlChars.Cr + ControlChars.Cr + " ")
While count > 0
Dim outputData As New [String](readBuff, 0, count)
Console.Write(outputData)
count = streamRead.Read(readBuff, 0, 256)
End While
' Close the Stream object.
streamResponse.Close()
streamRead.Close()
' Release the HttpWebResponse Resource.
myHttpWebResponse.Close()
Keterangan
Koleksi Headers berisi header protokol yang terkait dengan permintaan. Tabel berikut mencantumkan header HTTP yang tidak disimpan dalam Headers koleksi tetapi diatur oleh sistem atau diatur oleh properti atau metode.
Header | Diatur oleh |
---|---|
Terima | Diatur oleh Accept properti . |
Koneksi | Diatur oleh Connection properti dan KeepAlive properti . |
Panjang-Konten | Diatur oleh ContentLength properti . |
Jenis-Konten | Diatur oleh ContentType properti . |
Mengharapkan | Diatur oleh Expect properti . |
Tanggal | Diatur oleh Date properti . |
Host | Diatur oleh Host properti . |
If-Modified-Since | Diatur oleh IfModifiedSince properti . |
Rentang | Diatur oleh AddRange metode . |
Referer | Diatur oleh Referer properti . |
Pengodean-Transfer | Diatur oleh TransferEncoding properti ( SendChunked properti harus benar). |
Agen-Pengguna | Diatur oleh UserAgent properti . |
Metode ini Add melempar ArgumentException jika Anda mencoba mengatur salah satu header yang dilindungi ini.
Mengubah Headers properti setelah permintaan dimulai dengan memanggil GetRequestStreammetode , , BeginGetRequestStreamGetResponse, atau BeginGetResponse melemparkan InvalidOperationException.
Anda tidak boleh berasumsi bahwa nilai header akan tetap tidak berubah, karena server web dan cache dapat mengubah atau menambahkan header ke permintaan Web.
Berlaku untuk
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk