Baca dalam bahasa Inggris

Bagikan melalui


ServicePoint Kelas

Definisi

Menyediakan manajemen koneksi untuk koneksi HTTP.

public class ServicePoint
Warisan
ServicePoint

Contoh

Contoh kode berikut membuat objek ServicePoint yang tersambung ke URI www.contoso.com.

// This example shows how to use the ServicePoint and ServicePointManager classes.
// The ServicePointManager class uses the ServicePoint class to manage connections
// to a remote host. The networking classes reuse service points for all
// requests to a given URI. In fact, the same ServicePoint object
// is used to issue requests to Internet resources identified by the same
// scheme identifier (for example,  HTTP) and host fragment (for example,  www.contoso.com).
// This should improve your application performance.
// Reusing service points in this way can help improve application performance.
using System;
using System.Net;
using System.Threading;
using System.Text.RegularExpressions;

namespace Mssc.Services.ConnectionManagement
{
    class TestServicePoint
    {
        private static void ShowProperties(ServicePoint sp)
        {
            Console.WriteLine("Done calling FindServicePoint()...");

            // Display the ServicePoint Internet resource address.
            Console.WriteLine("Address = {0} ", sp.Address.ToString());

            // Display the date and time that the ServicePoint was last
            // connected to a host.
            Console.WriteLine("IdleSince = " + sp.IdleSince.ToString());

            // Display the maximum length of time that the ServicePoint instance
            // is allowed to maintain an idle connection to an Internet
            // resource before it is recycled for use in another connection.
            Console.WriteLine("MaxIdleTime = " + sp.MaxIdleTime);

            Console.WriteLine("ConnectionName = " + sp.ConnectionName);

            // Display the maximum number of connections allowed on this
            // ServicePoint instance.
            Console.WriteLine("ConnectionLimit = " + sp.ConnectionLimit);

            // Display the number of connections associated with this
            // ServicePoint instance.
            Console.WriteLine("CurrentConnections = " + sp.CurrentConnections);

            if (sp.Certificate == null)
                Console.WriteLine("Certificate = (null)");
            else
                Console.WriteLine("Certificate = " + sp.Certificate.ToString());

            if (sp.ClientCertificate == null)
                Console.WriteLine("ClientCertificate = (null)");
            else
                Console. WriteLine("ClientCertificate = " + sp.ClientCertificate.ToString());

            Console.WriteLine("ProtocolVersion = " + sp.ProtocolVersion.ToString());
            Console.WriteLine("SupportsPipelining = " + sp.SupportsPipelining);

            Console.WriteLine("UseNagleAlgorithm = " + sp.UseNagleAlgorithm.ToString());
            Console.WriteLine("Expect 100-continue = " + sp.Expect100Continue.ToString());
        }

        private static void makeWebRequest(int hashCode, string Uri)
        {
            HttpWebResponse res = null;

            // Make sure that the idle time has elapsed, so that a new
            // ServicePoint instance is created.
            Console.WriteLine("Sleeping for 2 sec.");
            Thread.Sleep(2000);
            try
            {
                // Create a request to the passed URI.
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Uri);

                Console.WriteLine("\nConnecting to " + Uri + " ............");

                // Get the response object.
                res = (HttpWebResponse)req.GetResponse();
                Console.WriteLine("Connected.\n");

                ServicePoint currentServicePoint = req.ServicePoint;

                // Display new service point properties.
                int currentHashCode = currentServicePoint.GetHashCode();

                Console.WriteLine("New service point hashcode: " + currentHashCode);
                Console.WriteLine("New service point max idle time: " + currentServicePoint.MaxIdleTime);
                Console.WriteLine("New service point is idle since " + currentServicePoint.IdleSince );

                // Check that a new ServicePoint instance has been created.
                if (hashCode == currentHashCode)
                    Console.WriteLine("Service point reused.");
                else
                    Console.WriteLine("A new service point created.") ;
            }
            catch (Exception e)
            {
                Console.WriteLine("Source : " + e.Source);
                Console.WriteLine("Message : " + e.Message);
            }
            finally
            {
                if (res != null)
                    res.Close();
            }
        }

        // Show the user how to use this program when wrong inputs are entered.
        private static void showUsage()
        {
            Console.WriteLine("Enter the proxy name as follows:");
            Console.WriteLine("\tcs_servicepoint proxyName");
        }

        public static void Main(string[] args)
        {
            int port = 80;

            // Define a regular expression to parse the user's input.
            // This is a security check. It allows only
            // alphanumeric input strings between 2 to 40 characters long.
            Regex rex = new Regex(@"^[a-zA-Z]\w{1,39}$");

            if (args.Length < 1)
            {
                showUsage();
                return;
            }
            string proxy = args[0];

            if (!(rex.Match(proxy)).Success)
            {
                Console.WriteLine("Input string format not allowed.");
                return;
            }
            string proxyAdd = "http://" + proxy + ":" + port;

            // Create a proxy object.
            WebProxy DefaultProxy = new WebProxy(proxyAdd, true);

            // Set the proxy that all HttpWebRequest instances use.
            WebRequest.DefaultWebProxy = DefaultProxy;

            // Get the base interface for proxy access for the
            // WebRequest-based classes.
            IWebProxy Iproxy = WebRequest.DefaultWebProxy;

            // Set the maximum number of ServicePoint instances to
            // maintain. If a ServicePoint instance for that host already
            // exists when your application requests a connection to
            // an Internet resource, the ServicePointManager object
            // returns this existing ServicePoint instance. If none exists
            // for that host, it creates a new ServicePoint instance.
            ServicePointManager.MaxServicePoints = 4;

            // Set the maximum idle time of a ServicePoint instance to 10 seconds.
            // After the idle time expires, the ServicePoint object is eligible for
            // garbage collection and cannot be used by the ServicePointManager object.
            ServicePointManager.MaxServicePointIdleTime = 10000;

            ServicePointManager.UseNagleAlgorithm = true;
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.CheckCertificateRevocationList = true;
            ServicePointManager.DefaultConnectionLimit = ServicePointManager.DefaultPersistentConnectionLimit;
            // Create the Uri object for the resource you want to access.
            Uri MS = new Uri("http://msdn.microsoft.com/");

            // Use the FindServicePoint method to find an existing
            // ServicePoint object or to create a new one.
            ServicePoint servicePoint = ServicePointManager.FindServicePoint(MS, Iproxy);

            ShowProperties(servicePoint);

            int hashCode = servicePoint.GetHashCode();

            Console.WriteLine("Service point hashcode: " + hashCode);

            // Make a request with the same scheme identifier and host fragment
            // used to create the previous ServicePoint object.
            makeWebRequest(hashCode, "http://msdn.microsoft.com/library/");
        }
    }
}

Keterangan

Perhatian

WebRequest, HttpWebRequest, ServicePoint, dan WebClient usang, dan Anda tidak boleh menggunakannya untuk pengembangan baru. Gunakan HttpClient sebagai gantinya.

Kelas ServicePoint menangani koneksi ke sumber daya Internet berdasarkan informasi host yang diteruskan di Pengidentifikasi Sumber Daya Seragam (URI) sumber daya sumber daya. Koneksi awal ke sumber daya menentukan informasi yang dipertahankan objek ServicePoint, yang kemudian dibagikan oleh semua permintaan berikutnya ke sumber daya tersebut.

ServicePoint objek dikelola oleh kelas ServicePointManager dan dibuat, jika perlu, dengan metode ServicePointManager.FindServicePoint. ServicePoint objek tidak pernah dibuat secara langsung tetapi selalu dibuat dan dikelola oleh kelas ServicePointManager. Jumlah maksimum objek ServicePoint yang dapat dibuat diatur oleh properti ServicePointManager.MaxServicePoints.

Setiap objek ServicePoint mempertahankan koneksinya ke sumber daya Internet hingga diam lebih lama dari waktu yang ditentukan dalam properti MaxIdleTime. Ketika ServicePoint melebihi nilai MaxIdleTime, ServicePoint dapat didaur ulang ke koneksi lain. Nilai default MaxIdleTime diatur oleh properti ServicePointManager.MaxServicePointIdleTime.

Ketika properti ConnectionLeaseTimeout diatur ke nilai selain -1, dan setelah waktu yang ditentukan berlalu, koneksi ServicePoint aktif ditutup setelah melayani permintaan berikutnya. Ini berguna untuk aplikasi yang tidak memerlukan koneksi aktif yang dibuka tanpa batas waktu, karena secara default.

Catatan

Dalam kondisi beban tinggi, beberapa aplikasi mungkin kehabisan utas gratis di ThreadPool, yang dapat menyebabkan performa sistem yang buruk (seperti waktu transaksi tinggi dan variabel).

Properti

Address

Mendapatkan Pengidentifikasi Sumber Daya Seragam (URI) server yang tersambung dengan objek ServicePoint ini.

BindIPEndPointDelegate

Menentukan delegasi untuk mengaitkan IPEndPoint lokal dengan ServicePoint.

Certificate

Mendapatkan sertifikat yang diterima untuk objek ServicePoint ini.

ClientCertificate

Mendapatkan sertifikat klien terakhir yang dikirim ke server.

ConnectionLeaseTimeout

Mendapatkan atau mengatur jumlah milidetik setelah koneksi ServicePoint aktif ditutup.

ConnectionLimit

Mendapatkan atau mengatur jumlah maksimum koneksi yang diizinkan pada objek ServicePoint ini.

ConnectionName

Mendapatkan nama koneksi.

CurrentConnections

Mendapatkan jumlah koneksi terbuka yang terkait dengan objek ServicePoint ini.

Expect100Continue

Mendapatkan atau menetapkan nilai Boolean yang menentukan apakah perilaku 100-Lanjutkan digunakan.

IdleSince

Mendapatkan tanggal dan waktu objek ServicePoint terakhir kali tersambung ke host.

MaxIdleTime

Mendapatkan atau mengatur jumlah waktu koneksi yang terkait dengan objek ServicePoint dapat tetap menganggur sebelum koneksi ditutup.

ProtocolVersion

Mendapatkan versi protokol HTTP yang digunakan objek ServicePoint.

ReceiveBufferSize

Mendapatkan atau mengatur ukuran buffer penerima untuk soket yang digunakan oleh ServicePointini.

SupportsPipelining

Menunjukkan apakah objek ServicePoint mendukung koneksi yang disalurkan.

UseNagleAlgorithm

Mendapatkan atau menetapkan nilai Boolean yang menentukan apakah algoritma Nagle digunakan pada koneksi yang dikelola oleh objek ServicePoint ini.

Metode

CloseConnectionGroup(String)

Menghapus grup koneksi yang ditentukan dari objek ServicePoint ini.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Mengembalikan nilai hash untuk instans ServicePoint.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
SetTcpKeepAlive(Boolean, Int32, Int32)

Mengaktifkan atau menonaktifkan opsi tetap hidup pada koneksi TCP.

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk