FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Improve web service request performance
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Improve web service request performance
Posted: Sat Dec 13, 2025 03:46 PM

Happy Saturday Everyone!

I am looking to improve the performance of the web request below. There are times when the request hangs causing a delay in getting data back to client software. Is there anything I can do to improve the code below?

Thank you in advance for your feedback!

TRY
oHttp := CreateObject( 'MSXML2.XMLHTTP' )
CATCH
oHttp := CreateObject( 'Microsoft.XMLHTTP' )
END
TRY
cURL := [https://...]
cCred := [user_id:password]
ENDIF
cHttpSend := [<?xml version="1.0"?>]
cHttpSend += [<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:xxx.com:05-OfferToCash:40-xxx">]
cHttpSend += [ <soapenv:Header/>]
cHttpSend += [ <soapenv:Body>]
cHttpSend += [ <urn:FindRFIDQry>]
cHttpSend += [ <!--Optional:-->]
cHttpSend += [ <Header>]
cHttpSend += [ <!--Optional:-->]
cHttpSend += [ <ShipmentDocument>123</ShipmentDocument>]
cHttpSend += [ <!--Optional:-->]
cHttpSend += [ <OutboundDelivery></OutboundDelivery>]
cHttpSend += [ </Header>]
cHttpSend += [ </urn:FindRFIDQry>]
cHttpSend += [ </soapenv:Body>]
cHttpSend += [</soapenv:Envelope>]

	oHttp:Open( 'POST', cURL, .F. )
	oHttp:SetRequestHeader( "Content-Type","application/xml")					// was /json
	oHttp:SetRequestHeader( "Accept","*/*")
	oHttp:SetRequestHeader( "Authorization", "Basic "+hb_base64Encode(cCred))
	oHttp:Send( cHttpSend )
	Sysrefresh()
	IF oHttp:status=200
		cWinResp := oHttp:ResponseBody
		MEMOWRIT( Pdrivelog + cSdno + '.XML', cWinResp )
	ENDIF
CATCH oError
	MEMOWRIT( Pdrivelog + 'WS-REQUEST-ERROR-' + cSdno + '.LOG', 'Unable to Complete Web Service' + cEol + oError:Description )
	RETURN (.F.)
END
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Improve web service request performance
Posted: Sun Dec 14, 2025 01:01 PM

Gemini 3 answer:

The reason your application "hangs" is specifically due to the third parameter in your Open method:

Code (xbase): Select all Collapse
oHttp:Open( 'POST', cURL, .F. )

The .F. sets the request to Synchronous Mode. This means your application stops completely and waits for the server to reply. If the server is slow, or the network drops packets, your software will freeze indefinitely until a default system timeout occurs (which can take a long time).

Here are three concrete steps to fix this without rewriting your entire logic flow.

1\. Switch to ServerXMLHTTP

The standard Microsoft.XMLHTTP object is designed for old browser clients (like IE) and relies on the browser's cache and proxy settings. It is often unstable for application-to-application communication.

You should use MSXML2.ServerXMLHTTP. It is more robust, thread-safe, and most importantly, it allows you to set explicit timeouts.

2\. Set Explicit Timeouts

By switching to the server object, you can tell the system: "If you don't connect in 5 seconds, give up." This prevents the "eternal hang."

3\. Revised Code

Here is the improved version of your code. I have updated the object creation and added the timeout logic.

Code (xbase): Select all Collapse
TRY
    // Use ServerXMLHTTP for better reliability and timeout support
    // We try 6.0 first (most modern), then fall back
    TRY
        oHttp := CreateObject( 'MSXML2.ServerXMLHTTP.6.0' )
    CATCH
        oHttp := CreateObject( 'MSXML2.ServerXMLHTTP' )
    END

    // VITAL: Set Timeouts (in milliseconds)
    // Syntax: .setTimeouts( resolveTimeout, connectTimeout, sendTimeout, receiveTimeout )
    // Example below: 5s to resolve DNS, 5s to connect, 15s to send, 15s to receive response
    oHttp:setTimeouts( 5000, 5000, 15000, 15000 )

    cURL      := "https://..." 
    cCred     := "user_id:password"
    
    // ... (Your XML construction remains the same) ...
    cHttpSend := [<?xml version="1.0"?>]
    // ... code truncated for brevity ...
    cHttpSend += [</soapenv:Envelope>]

    // We keep .F. (Synchronous), but now it is safe because of the Timeouts above.
    oHttp:Open( 'POST', cURL, .F. )
    
    oHttp:SetRequestHeader( "Content-Type", "application/xml" )
    oHttp:SetRequestHeader( "Accept", "*/*" )
    oHttp:SetRequestHeader( "Authorization", "Basic " + hb_base64Encode(cCred) )
    
    // The app will wait here, but ONLY for the duration of the timeout set above.
    oHttp:Send( cHttpSend )

    // Sysrefresh is useful to redraw the screen, but it only runs AFTER the send finishes.
    Sysrefresh() 

    IF oHttp:status == 200
        cWinResp := oHttp:ResponseBody
        MEMOWRIT( Pdrivelog + cSdno + '.XML', cWinResp )
    ELSE
        // Handle HTTP errors (like 404 or 500) that are not exceptions
        MEMOWRIT( Pdrivelog + 'WS-HTTP-ERROR-' + cSdno + '.LOG', 'HTTP Status: ' + Str(oHttp:status) + ' ' + oHttp:statusText )
    ENDIF

CATCH oError
    // This catches both Object creation errors AND Timeout errors
    MEMOWRIT( Pdrivelog + 'WS-REQUEST-ERROR-' + cSdno + '.LOG', 'Unable to Complete Web Service' + cEol + oError:Description )
    RETURN (.F.)
END

Summary of Changes

  1. Object: Changed to MSXML2.ServerXMLHTTP.6.0.
  2. Safety: Added oHttp:setTimeouts(...).
  3. Error Handling: Added an ELSE block to the status check. Sometimes a request "succeeds" technically (no crash) but returns a 500 Internal Server Error. You want to log that, too.

A Note on Sysrefresh()

You currently have Sysrefresh() after the oHttp:Send(). Because the request is synchronous (.F.), Sysrefresh() will not execute until the download is finished.

  • If you want the UI to remain clickable/moving while downloading: You would need to change .F. to .T. (Asynchronous) and use an event handler (OnReadyStateChange). However, that is significantly more complex to implement in Harbour/xBase.
  • Recommendation: Stick with the code above. The timeouts ensure that if the internet is slow, the app only "hangs" for 15 seconds (or whatever you set) rather than forever.

-----
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: Improve web service request performance
Posted: Sun Dec 14, 2025 07:01 PM

Hello Antonio,

Thank you so much!

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com

Continue the discussion