Resolved OpenEdge HTTP client using Auto Detect Proxy

Cecil

19+ years progress programming and still learning.
Interesting problem to do with internet proxy. Depending on if the end user (Windows) is inside the organization network or exsternal with or without VPN, the access determines if they have access to a proxy server and which one.

I have developed application which uses the OpenEdge HTTP client and it needs to dynamically use the a proxy server depending on the resource location.

I have written a function to try and auto detect which proxy server to use based on it's target URL location but it not working as expected as it's always returning false.

Code:
//USING System.Net.WebProxy.
//USING System.Uri.
//System.Net.WebProxy.

FUNCTION isProxyRequired RETURN LOGICAL (INPUT pchResource        AS CHARACTER,
                                         OUTPUT opchResourceProxy AS CHARACTER):

    DEFINE VARIABLE objWebProxy        AS CLASS  System.Net.WebProxy  NO-UNDO.
    DEFINE VARIABLE objResourceProxy   AS CLASS  System.Uri           NO-UNDO.
    DEFINE VARIABLE objResource        AS CLASS  System.Uri           NO-UNDO.
    DEFINE VARIABLE lgIsProxyRequired  AS LOGICAL                     NO-UNDO.

    objResource = NEW System.Uri(pchResource).
    objWebProxy = NEW System.Net.WebProxy().

    objResourceProxy = objWebProxy:getProxy( objResource ).
    
    MESSAGE
        objWebProxy:address SKIP
        objWebProxy:BypassProxyOnLocal    SKIP
        objWebProxy:BypassList
        
        VIEW-AS ALERT-BOX INFO.

    IF objResourceProxy EQ objResource THEN
        ASSIGN
            lgIsProxyRequired = FALSE.
    ELSE
        ASSIGN
            lgIsProxyRequired = TRUE
            opchResourceProxy = objResourceProxy:ToString().
    
    RETURN lgIsProxyRequired.
    
    
    //Garbage collection
    FINALLY:

        IF VALID-OBJECT(objWebProxy) THEN
            DELETE OBJECT objWebProxy.
            
        IF VALID-OBJECT(objResource) THEN
            DELETE OBJECT objResource.
            
        IF VALID-OBJECT(objResourceProxy) THEN
            DELETE OBJECT objResourceProxy.   
            
            
    END FINALLY.

END FUNCTION.

DEFINE VARIABLE chProxyServer AS CHARACTER   NO-UNDO.

MESSAGE isProxyRequired("http://google.com",
                        OUTPUT chProxyServer) SKIP
                        chProxyServer.
 

Cecil

19+ years progress programming and still learning.
Took me a little while to figure out but thanks to ChatGPT.

Code:
    method public static logical ProxyAutoDetection(input BaseURI as character  ):
       
        define variable ProxyServerURI as character no-undo.
        define variable BypassedProxy  as logical   no-undo.
        define variable Uri            as class     System.Uri no-undo.
       
       
        // Get the system's default proxy
        WebProxy = WebRequest:DefaultWebProxy.
       
        // If the proxy is not null and is enabled
        if  valid-object(WebProxy) and WebProxy ne WebRequest:GetSystemWebProxy() then        
        do:
           
            Uri = new System.Uri(BaseURI).
           
            ProxyServerURI = WebProxy:GetProxy(Uri):ToString().
           
            BypassedProxy = WebProxy:IsBypassed( Uri ).
           
            // Display the proxy settings
           
            message "Proxy Settings".
           
            message BaseURI.
           
            message substitute("Proxy Server: &1", ProxyServerURI ).
               
            message substitute("Bypass Proxy: &1", string(BypassedProxy, "YES/NO") ).  
            .  
               
            if ProxyServerURI ne "" and not BypassedProxy then
            do:
                assign
                    Classes.HTTP.Proxy:UseProxy = true    
                    Classes.HTTP.Proxy:Host     = ProxyServerURI.
                return true.
            end.  
            else
            do:
                assign
                    Classes.HTTP.Proxy:UseProxy = false    
                    Classes.HTTP.Proxy:Host     = ''.
                   
                return false.
            end.      
        end.
        else
        do:
           
            message "No proxy detected, or proxy is not configured.".
            return false.
        end.
       
        finally:
       
            if valid-object(WebProxy) then
                delete object (WebProxy).
           
        end finally.
       

    end method.
 
Top