Feedback
 
Did this article resolve your question/issue?

   

Your feedback is appreciated.

Please tell us how we can make this article more useful. Please provide us a way to contact you, should we need clarification on the feedback provided or if you need further assistance.

Characters Remaining: 1025

 


Article

How to set the Connection Timeout for a request using OpenEdge HttpClient?

« Go Back

Information

 
Article Number000096511
EnvironmentProduct: OpenEdge
Version: 11.5.1,11.6,11.7,12.x
OS: All Supported Platforms
Other: HttpClient
Question/Problem Description
The below code demonstrates setting a connection timeout in milliseconds for the HttpClient.
 
Steps to Reproduce
Clarifying Information
Error Message
Defect/Enhancement Number
Cause
Resolution
/*------------------------------------------------------------------------
    File        : set_connect_timeout.p
    Purpose     : Shows how to set a timeout (in milliseconds) for the HTTP Client's CONNECT 
  ----------------------------------------------------------------------*/
BLOCK-LEVEL ON ERROR UNDO, THROW.

USING HttpClient.ConnectCSCP.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.IHttpClient.
USING OpenEdge.Net.HTTP.IHttpClientLibrary.
USING OpenEdge.Net.HTTP.Lib.ClientLibraryBuilder.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.ServerConnection.ClientSocket.
USING OpenEdge.Net.ServerConnection.ClientSocketConnectionParameters.

/* ***************************  Settings *************************** */
SESSION:ERROR-STACK-TRACE = TRUE.

/* ***************************  Main Block  *************************** */
DEFINE VARIABLE oClient AS IHttpClient            NO-UNDO.
DEFINE VARIABLE oLib    AS IHttpClientLibrary     NO-UNDO.
DEFINE VARIABLE oSocket AS ClientSocket           NO-UNDO.
DEFINE VARIABLE oCSCP   AS HttpClient.ConnectCSCP NO-UNDO.

       // create a custom connection param with connect-timeout
ASSIGN oCSCP                = NEW HttpClient.ConnectCSCP()
       oCSCP:ConnectTimeout = 1000
       // tell the abl-socket-library to use the new connection params
       oLib = ClientLibraryBuilder:Build()
                    // use the custom ConnectionParameters 
                    :Option(GET-CLASS(ClientSocketConnectionParameters):TypeName,
                            oCSCP)
                    :Library
       // tell the client to use the custom library
       oClient = ClientBuilder:Build()
                    :UsingLibrary(oLib)
                    :Client.
// there's no server here
oClient:Execute(RequestBuilder:Get('http://localhost:9999'):Request).

CATCH oError AS Progress.Lang.Error :
    MESSAGE oError:GetMessage(1) SKIP(2)
            oError:CallStack
        VIEW-AS ALERT-BOX.
END CATCH.
 
/*------------------------------------------------------------------------
    File        : ConnectCSCP
    Description : Connection Parameters with the Connect Timeout 
    Notes       : Save this as HttpClient/ConnectCSCP.cls
  ----------------------------------------------------------------------*/
BLOCK-LEVEL ON ERROR UNDO, THROW.

USING OpenEdge.Core.Assert.
USING OpenEdge.Net.ServerConnection.ClientSocketConnectionParameters.
USING OpenEdge.Net.URI.

CLASS ConnectCSCP INHERITS ClientSocketConnectionParameters:
    /* The timeout for the socket CONNECT, in milliseconds */
    DEFINE PUBLIC PROPERTY ConnectTimeout AS INTEGER NO-UNDO
        GET.
        SET(INPUT pTimeout AS INTEGER):
            Assert:NotNull(pTimeout, 'Connect timeout').
            THIS-OBJECT:ConnectTimeout = pTimeout.
        END SET.
    
    CONSTRUCTOR PUBLIC ConnectCSCP (  ):
        SUPER ().
    END CONSTRUCTOR.
    
    CONSTRUCTOR PUBLIC ConnectCSCP ( INPUT pScheme AS CHARACTER, 
                                     INPUT pHost   AS CHARACTER, 
                                     INPUT pPort   AS INTEGER ):
        SUPER (INPUT pScheme, INPUT pHost, INPUT pPort).
    END CONSTRUCTOR.
        
    CONSTRUCTOR PUBLIC ConnectCSCP ( INPUT poURI AS URI ):
        SUPER (INPUT poURI).
    END CONSTRUCTOR.

    METHOD OVERRIDE PUBLIC CHARACTER GetConnectionString():
        DEFINE VARIABLE cConnectionParams AS CHARACTER NO-UNDO.
        
        cConnectionParams = SUPER:GetConnectionString().
        IF THIS-OBJECT:ConnectTimeout GT 0 THEN
            cConnectionParams = SUBSTITUTE('&1 -clientConnectTimeout &2':u,
                                                                        cConnectionParams, ConnectTimeout).
        
        RETURN cConnectionParams.
    END METHOD.    

END CLASS.

 
Workaround
Notes
References to Other Documentation:

Progress Article(s):
000060873, Is there an HTTP client for OpenEdge?
000095515, OpenEdge.Net.pl is missing from PROPATH

​​​​​​​
Attachment 
Last Modified Date6/20/2019 6:28 PM