ServicePoint.ReceiveBufferSize 屬性

定義

取得或設定這個 ServicePoint所使用的套接字接收緩衝區大小。

public int ReceiveBufferSize { get; set; }

屬性值

Int32,其中包含接收緩衝區的大小,以位元組為單位。 預設值為8192。

例外狀況

設定作業指定的值大於 Int32.MaxValue

範例

下列程式代碼範例會設定這個屬性的值。

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Examples.System.Net
{
    public class ServicePointExample
    {
        // Pass in the name of the Web page to retrieve.
        public static void Main(string[] args)
        {
            string page;
            if (args == null || args.Length == 0 || args[0].Length == 0)
            {
                page = "http://www.contoso.com/default.html";
            }
            else
            {
                page = args[0];
            }
            // Create the request.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
            // Get the service point that handles the request's socket connection.
            ServicePoint point = request.ServicePoint;
            // Set the receive buffer size on the underlying socket.
            point.ReceiveBufferSize = 2048;
            // Set the connection lease timeout to infinite.
            point.ConnectionLeaseTimeout = Timeout.Infinite;
            // Send the request.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader s = new StreamReader(responseStream);
            // Display the response.
            Console.WriteLine(s.ReadToEnd());
            s.Close();
            responseStream.Close();
            response.Close();
        }
    }
}

備註

警告

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

如需詳細資訊,請參閱 ReceiveBufferSize

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1