ServicePoint Sınıf
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
HTTP bağlantıları için bağlantı yönetimi sağlar.
public ref class ServicePoint
public class ServicePoint
type ServicePoint = class
Public Class ServicePoint
- Devralma
-
ServicePoint
Örnekler
Aşağıdaki kod örneği, www.contoso.com
URI'sine bağlanan bir ServicePoint nesnesi oluşturur.
// 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.dll>
using namespace System;
using namespace System::Net;
using namespace System::Threading;
using namespace System::Text::RegularExpressions;
void ShowProperties( ServicePoint^ sp )
{
Console::WriteLine( "Done calling FindServicePoint()..." );
// Display the ServicePoint Internet resource address.
Console::WriteLine( "Address = {0}", sp->Address );
// Display the date and time that the ServicePoint was last
// connected to a host.
Console::WriteLine( "IdleSince = {0}", sp->IdleSince );
// 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 = {0}", sp->MaxIdleTime );
Console::WriteLine( "ConnectionName = {0}", sp->ConnectionName );
// Display the maximum number of connections allowed on this
// ServicePoint instance.
Console::WriteLine( "ConnectionLimit = {0}", sp->ConnectionLimit );
// Display the number of connections associated with this
// ServicePoint instance.
Console::WriteLine( "CurrentConnections = {0}", sp->CurrentConnections );
if ( sp->Certificate == nullptr )
Console::WriteLine( "Certificate = (null)" );
else
Console::WriteLine( "Certificate = {0}", sp->Certificate );
if ( sp->ClientCertificate == nullptr )
Console::WriteLine( "Client Certificate = (null)" );
else
Console::WriteLine( "Client Certificate = {0}", sp->ClientCertificate );
Console::WriteLine( "ProtocolVersion = {0}", sp->ProtocolVersion->ToString() );
Console::WriteLine( "SupportsPipelining = {0}", sp->SupportsPipelining );
Console::WriteLine( "UseNagleAlgorithm = {0} ", sp->UseNagleAlgorithm.ToString() );
Console::WriteLine( "Expect 100-continue = {0}", sp->Expect100Continue.ToString() );
}
void makeWebRequest( int hashCode, String^ Uri )
{
HttpWebResponse^ res = nullptr;
// 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 = dynamic_cast<HttpWebRequest^>(WebRequest::Create( Uri ));
Console::WriteLine( "\nConnecting to {0} ............", Uri );
// Get the response object.
res = dynamic_cast<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: {0}", currentHashCode );
Console::WriteLine( "New service point max idle time: {0}", currentServicePoint->MaxIdleTime );
Console::WriteLine( "New service point is idle since {0}", 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 : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
finally
{
if ( res != nullptr )
res->Close();
}
}
// Show the user how to use this program when wrong inputs are entered.
void showUsage()
{
Console::WriteLine( "Enter the proxy name as follows:" );
Console::WriteLine( "\tcs_servicepoint proxyName" );
}
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
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 = gcnew Regex( "^[a-zA-Z]\\w{1,39}$" );
if ( args->Length < 2 )
{
showUsage();
return -1;
}
String^ proxy = args[ 1 ];
if ( (rex->Match(proxy))->Success != true )
{
Console::WriteLine( "Input string format not allowed." );
return -1;
}
String^ proxyAdd = String::Format( "http://{0}:{1}", proxy, port );
// Create a proxy object.
WebProxy^ DefaultProxy = gcnew 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.
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 = gcnew 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: {0}", 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/" );
}
// 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 != true)
{
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/");
}
}
}
' 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).
' Reusing service points in this way can help improve application performance.
Imports System.Net
Imports System.Threading
Imports System.Text.RegularExpressions
Namespace Mssc.Services.ConnectionManagement
Module M_TestServicePoint
Class TestServicePoint
Private Shared Sub ShowProperties(ByVal sp As ServicePoint)
Console.WriteLine("Done calling FindServicePoint")
' Display the ServicePoint Internet resource address.
Console.WriteLine(("Address = " + 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.ToString()))
Console.WriteLine(("ConnectionName = " + sp.ConnectionName))
' Display the maximum number of connections allowed on this
' ServicePoint instance.
Console.WriteLine(("ConnectionLimit = " + sp.ConnectionLimit.ToString()))
' Display the number of connections associated with this
' ServicePoint instance.
Console.WriteLine(("CurrentConnections = " + sp.CurrentConnections.ToString()))
If sp.Certificate Is Nothing Then
Console.WriteLine("Certificate = (null)")
Else
Console.WriteLine(("Certificate = " + sp.Certificate.ToString()))
End If
If sp.ClientCertificate Is Nothing Then
Console.WriteLine("ClientCertificate = (null)")
Else
Console.WriteLine(("ClientCertificate = " + sp.ClientCertificate.ToString()))
End If
Console.WriteLine("ProtocolVersion = " + sp.ProtocolVersion.ToString())
Console.WriteLine(("SupportsPipelining = " + sp.SupportsPipelining.ToString()))
Console.WriteLine("UseNagleAlgorithm = " + sp.UseNagleAlgorithm.ToString())
Console.WriteLine("Expect 100-continue = " + sp.Expect100Continue.ToString())
End Sub
Private Shared Sub makeWebRequest(ByVal hashCode As Integer, ByVal Uri As String)
Dim res As HttpWebResponse = Nothing
' 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.
Dim req As HttpWebRequest = CType(WebRequest.Create(Uri), HttpWebRequest)
Console.WriteLine((ControlChars.Lf + "Connecting to " + Uri + " ............"))
' Get the response object.
res = CType(req.GetResponse(), HttpWebResponse)
Console.WriteLine("Connected." + ControlChars.Lf)
Dim currentServicePoint As ServicePoint = req.ServicePoint
' Display new service point properties.
Dim currentHashCode As Integer = currentServicePoint.GetHashCode()
Console.WriteLine(("New service point hashcode: " + currentHashCode.ToString()))
Console.WriteLine(("New service point max idle time: " + currentServicePoint.MaxIdleTime.ToString()))
Console.WriteLine(("New service point is idle since " + currentServicePoint.IdleSince.ToString()))
' Check that a new ServicePoint instance has been created.
If hashCode = currentHashCode Then
Console.WriteLine("Service point reused.")
Else
Console.WriteLine("A new service point created.")
End If
Catch e As Exception
Console.WriteLine(("Source : " + e.Source))
Console.WriteLine(("Message : " + e.Message))
Finally
If Not (res Is Nothing) Then
res.Close()
End If
End Try
End Sub
' Show the user how to use this program when wrong inputs are entered.
Private Shared Sub showUsage()
Console.WriteLine("Enter the proxy name as follows:")
Console.WriteLine(ControlChars.Tab + "vb_servicepoint proxyName")
End Sub
' This is the program entry point. It allows the user to enter
' a server name that is used to locate its current homepage.
Public Shared Sub Main(ByVal args() As String)
Dim proxy As String = Nothing
Dim port As Integer = 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.
Dim rex As New Regex("^[a-zA-Z]\w{1,39}$")
If args.Length = 0 Then
' Show how to use this program.
showUsage()
Return
End If
proxy = args(0)
If (Not (rex.Match(proxy)).Success) Then
Console.WriteLine("Input string format not allowed.")
Return
End If
' Create a proxy object.
Dim proxyAdd As String
proxyAdd = "http://" + proxy + ":" + port.ToString()
Dim DefaultProxy As 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.
Dim Iproxy As IWebProxy = WebRequest.DefaultWebProxy
' Set the maximum number of ServicePoint instances to maintain.
' Note that, 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. 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.
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.
Dim MS As New Uri("http://msdn.microsoft.com/")
' Use the FindServicePoint method to find an existing
' ServicePoint object or to create a new one.
Dim servicePoint As ServicePoint = ServicePointManager.FindServicePoint(MS, Iproxy)
ShowProperties(servicePoint)
Dim hashCode As Integer = servicePoint.GetHashCode()
Console.WriteLine(("Service point hashcode: " + hashCode.ToString()))
' 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/")
End Sub
End Class
End Module
End Namespace
Açıklamalar
Dikkat
WebRequest
, HttpWebRequest
, ServicePoint
ve WebClient
kullanım dışıdır ve bunları yeni geliştirme için kullanmamalısınız. Bunun yerine HttpClient kullanın.
ServicePoint sınıfı, kaynağın Tekdüzen Kaynak Tanımlayıcısı'nda (URI) geçirilen konak bilgilerine göre bir İnternet kaynağına bağlantıları işler. Kaynakla ilk bağlantı, ServicePoint nesnesinin koruduğu bilgileri belirler ve daha sonra bu kaynağa yönelik sonraki tüm istekler tarafından paylaşılır.
ServicePoint nesneleri ServicePointManager sınıfı tarafından yönetilir ve gerekirse ServicePointManager.FindServicePoint yöntemiyle oluşturulur. ServicePoint nesneleri hiçbir zaman doğrudan oluşturulmaz ancak her zaman ServicePointManager sınıfı tarafından oluşturulur ve yönetilir. Oluşturulabilecek en fazla ServicePoint nesne sayısı, ServicePointManager.MaxServicePoints özelliği tarafından ayarlanır.
Her ServicePoint nesnesi, MaxIdleTime özelliğinde belirtilen süreden daha uzun süre boşta kalana kadar bir İnternet kaynağıyla bağlantısını korur. bir ServicePointMaxIdleTime değerini aştığında, başka bir bağlantıya geri dönüştürülebilir. varsayılan MaxIdleTime değeri ServicePointManager.MaxServicePointIdleTime özelliği tarafından ayarlanır.
ConnectionLeaseTimeout özelliği -1 dışında bir değere ayarlandığında ve belirtilen süre geçtikten sonra, bir sonraki isteği karşıladıktan sonra etkin bir ServicePoint bağlantısı kapatılır. Bu, varsayılan olarak olduğu gibi süresiz olarak açılan etkin bağlantılar gerektirmeyen uygulamalar için kullanışlıdır.
Not
Yüksek yük koşullarında, bazı uygulamalarda ThreadPool'da boş iş parçacıkları tükenebilir ve bu da sistem performansının düşmesine (yüksek ve değişken işlem süreleri gibi) yol açabilir.
Özellikler
Address |
Bu ServicePoint nesnesinin bağlandığını sunucunun Tekdüzen Kaynak Tanımlayıcısını (URI) alır. |
BindIPEndPointDelegate |
Yerel IPEndPoint bir ServicePointile ilişkilendirilecek temsilciyi belirtir. |
Certificate |
Bu ServicePoint nesnesi için alınan sertifikayı alır. |
ClientCertificate |
Sunucuya gönderilen son istemci sertifikasını alır. |
ConnectionLeaseTimeout |
Etkin bir ServicePoint bağlantısının kapatıldığı milisaniye sayısını alır veya ayarlar. |
ConnectionLimit |
Bu ServicePoint nesnesinde izin verilen en fazla bağlantı sayısını alır veya ayarlar. |
ConnectionName |
Bağlantı adını alır. |
CurrentConnections |
Bu ServicePoint nesnesiyle ilişkili açık bağlantı sayısını alır. |
Expect100Continue |
100-Devam davranışının kullanılıp kullanılmadığını belirleyen bir Boolean değeri alır veya ayarlar. |
IdleSince |
ServicePoint nesnesinin bir konağa en son bağlanıldığı tarihi ve saati alır. |
MaxIdleTime |
ServicePoint nesnesiyle ilişkilendirilmiş bir bağlantının, bağlantı kapatılmadan önce boşta kalabileceği süreyi alır veya ayarlar. |
ProtocolVersion |
ServicePoint nesnesinin kullandığı HTTP protokolünün sürümünü alır. |
ReceiveBufferSize |
Bu ServicePointtarafından kullanılan yuva için alma arabelleğinin boyutunu alır veya ayarlar. |
SupportsPipelining |
ServicePoint nesnesinin işlem hattı bağlantılarını destekleyip desteklemediğini gösterir. |
UseNagleAlgorithm |
Nagle algoritmasının bu ServicePoint nesnesi tarafından yönetilen bağlantılarda kullanılıp kullanılmadığını belirleyen bir Boolean değeri alır veya ayarlar. |
Yöntemler
CloseConnectionGroup(String) |
Belirtilen bağlantı grubunu bu ServicePoint nesnesinden kaldırır. |
Equals(Object) |
Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler. (Devralındığı yer: Object) |
GetHashCode() |
ServicePoint örneği için bir karma değeri döndürür. |
GetHashCode() |
Varsayılan karma işlevi işlevi görür. (Devralındığı yer: Object) |
GetType() |
Geçerli örneğin Type alır. (Devralındığı yer: Object) |
MemberwiseClone() |
Geçerli Objectbasit bir kopyasını oluşturur. (Devralındığı yer: Object) |
SetTcpKeepAlive(Boolean, Int32, Int32) |
TCP bağlantısında etkin tutma seçeneğini etkinleştirir veya devre dışı bırakır. |
ToString() |
Geçerli nesneyi temsil eden bir dize döndürür. (Devralındığı yer: Object) |