Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

HttpWebRequest.ServicePoint Property

Gets the service point to use for the request.

[Visual Basic]
Public ReadOnly Property ServicePoint As ServicePoint
[C#]
public ServicePoint ServicePoint {get;}
[C++]
public: __property ServicePoint* get_ServicePoint();
[JScript]
public function get ServicePoint() : ServicePoint;

Property Value

A ServicePoint that represents the network connection to the Internet resource.

Remarks

The ServicePoint.Address property may be different from HttpWebRequest.Address if the request is redirected.

Example

[Visual Basic] 
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))

        ' 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 'makeWebRequest


[C#] 
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 ();
    }
}


[C++] 
void makeWebRequest(int hashCode, String* Uri)
{
    HttpWebResponse* res = 0;

    // Make sure that the idle time has elapsed, so that a new 
    // ServicePoint instance is created.
    Console::WriteLine(S"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(S"\nConnecting to {0} ............", Uri);

        // Get the response object.
        res = dynamic_cast<HttpWebResponse*>(req->GetResponse());
        Console::WriteLine(S"Connected.\n");
        ServicePoint* currentServicePoint = req->ServicePoint;

        // Display new service point properties.
        int currentHashCode = currentServicePoint->GetHashCode();
        Console::WriteLine(S"New service point hashcode: {0}", __box(currentHashCode));
        Console::WriteLine(S"New service point max idle time: {0}", __box(currentServicePoint->MaxIdleTime));
        Console::WriteLine(S"New service point is idle since {0}", __box(currentServicePoint->IdleSince));

        // Check that a new ServicePoint instance has been created.
        if (hashCode == currentHashCode)
            Console::WriteLine(S"Service point reused.");
        else
            Console::WriteLine(S"A new service point created.");
    }
    catch(Exception* e)
    {
        Console::WriteLine(S"Source : {0}", e->Source);
        Console::WriteLine(S"Message : {0}", e->Message);
    }
    __finally
    {
        if (res != 0)
            res->Close();
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

HttpWebRequest Class | HttpWebRequest Members | System.Net Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.