HttpWebRequest.MaximumResponseHeadersLength 属性      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置响应标头允许的最大长度。
public:
 property int MaximumResponseHeadersLength { int get(); void set(int value); };public int MaximumResponseHeadersLength { get; set; }member this.MaximumResponseHeadersLength : int with get, setPublic Property MaximumResponseHeadersLength As Integer属性值
响应标头的长度(以 KB 为单位)(1024 字节)。
例外
在提交请求后设置该属性。
该值小于 0。
示例
下面的代码示例设置此属性的值。
using System;
using System.Net;
using System.Text;
using System.IO;
    public class Test
    {
        // Specify the URL to receive the request.
        public static void Main (string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine("Content length is {0}", response.ContentLength);
            Console.WriteLine("Content type is {0}", response.ContentType);
            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();
            // Pipes the stream to a higher level stream reader with the required encoding format.
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            Console.WriteLine("Response stream received.");
            Console.WriteLine(readStream.ReadToEnd());
            response.Close();
            readStream.Close();
        }
    }
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
Imports System.Net
Imports System.Text
Imports System.IO
    Public Class Test
        ' Specify the URL to receive the request.
        Public Shared Sub Main(ByVal args() As String)
        Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
        ' Set some reasonable limits on resources used by this request
        request.MaximumAutomaticRedirections = 4
        request.MaximumResponseHeadersLength = 4
        ' Set credentials to use for this request.
        request.Credentials = CredentialCache.DefaultCredentials
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Console.WriteLine("Content length is {0}", response.ContentLength)
        Console.WriteLine("Content type is {0}", response.ContentType)
        ' Get the stream associated with the response.
        Dim receiveStream As Stream = response.GetResponseStream()
        ' Pipes the stream to a higher level stream reader with the required encoding format. 
        Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
        Console.WriteLine("Response stream received.")
        Console.WriteLine(readStream.ReadToEnd())
        response.Close()
        readStream.Close()
    End Sub
End Class
'
'The output from this example will vary depending on the value passed into Main 
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'...
'
'
注解
谨慎
              WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
响应标头的长度包括响应状态行以及作为 HTTP 协议的一部分接收的任何额外控制字符。 值为 0 表示所有请求都失败。
如果未显式设置 MaximumResponseHeadersLength 属性,则默认为 DefaultMaximumResponseHeadersLength 属性的值。
如果收到的响应标头的长度超过 MaximumResponseHeadersLength 属性的值,则 EndGetResponse 或 GetResponse 方法将引发 WebException,并将 Status 属性设置为 MessageLengthLimitExceeded。