Export (0) Print
Expand All

ServicePointManager::FindServicePoint Method (String, IWebProxy)

Finds an existing ServicePoint object or creates a new ServicePoint object to manage communications with the specified Uniform Resource Identifier (URI).

Namespace:  System.Net
Assembly:  System (in System.dll)

public:
static ServicePoint^ FindServicePoint(
	String^ uriString, 
	IWebProxy^ proxy
)

Parameters

uriString
Type: System::String

The URI of the Internet resource to be contacted.

proxy
Type: System.Net::IWebProxy

The proxy data for this request.

Return Value

Type: System.Net::ServicePoint
The ServicePoint object that manages communications for the request.

ExceptionCondition
UriFormatException

The URI specified in uriString is invalid.

InvalidOperationException

The maximum number of ServicePoint objects defined in MaxServicePoints has been reached.

The FindServicePoint method returns the ServicePoint object associated with the specified Internet host name. If no ServicePoint object exists for that host, the ServicePointManager object creates one.

The following code example demonstrates calling this method to access a ServicePoint object.

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/" );
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft