WebClient.Headers 属性
定义
获取或设置与请求关联的标头名称/值对集合。Gets or sets a collection of header name/value pairs associated with the request.
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
属性值
一个 WebHeaderCollection,包含与此请求关联的标头名称/值对。A WebHeaderCollection containing header name/value pairs associated with this request.
示例
下面的代码示例使用 Headers 集合将 HTTP Content-Type 标头设置为, application/x-www-form-urlencoded, 以通知服务器窗体数据已附加到 post。The following code example uses the Headers collection to set the HTTP Content-Type header to application/x-www-form-urlencoded, to notify the server that form data is attached to the post.
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))
注解
Headers属性包含一个实例,该 WebHeaderCollection 实例包含 WebClient 与请求一起发送的协议标头。The Headers property contains a WebHeaderCollection instance containing protocol headers that the WebClient sends with the request.
某些常用标头被视为受限制,并受系统保护,无法在对象中设置或更改 WebHeaderCollection 。Some common headers are considered restricted and are protected by the system and cannot be set or changed in a WebHeaderCollection object. 尝试发送请求时,任何在与对象关联的对象中设置其中一个受限制的标头的尝试 WebHeaderCollection WebClient 都将引发异常 WebClient 。Any attempt to set one of these restricted headers in the WebHeaderCollection object associated with a WebClient object will throw an exception later when attempting to send the WebClient request.
受限制的系统标头包括但不限于以下各项:Restricted headers protected by the system include, but are not limited to the following:
DateDate
主机Host
此外,在使用对象时,还限制了其他一些标头 WebClient 。In addition, some other headers are also restricted when using a WebClient object. 这些受限制的标头包括但不限于以下各项:These restricted headers include, but are not limited to the following:
AcceptAccept
连接Connection
Content-LengthContent-Length
如果将值设置为 "100-continue",则应 (Expect (when the value is set to "100-continue"
If-Modified-SinceIf-Modified-Since
范围Range
Transfer-EncodingTransfer-Encoding
HttpWebRequest类具有设置上述一些标头的属性。The HttpWebRequest class has properties for setting some of the above headers. 如果对应用程序设置这些标头很重要,则 HttpWebRequest 应使用类,而不是 WebRequest 类。If it is important for an application to set these headers, then the HttpWebRequest class should be used instead of the WebRequest class.
不应假定标头值保持不变,因为 Web 服务器和缓存可能会更改或向 Web 请求添加标头。You should not assume that the header values will remain unchanged, because Web servers and caches may change or add headers to a Web request.