如何用Sybase PowerBuilder 11.5对接REST API获取数据?
Fetching Data from REST API in PowerBuilder 11.5
Hey there! Since PowerBuilder 11.5 doesn't include native support for REST APIs, you'll need to rely on OLE objects to handle HTTP requests. Below are two straightforward, reliable methods to pull data from your target endpoint:
Method 1: Use MSXML2.XMLHTTP
This is a widely supported option on Windows systems, leveraging the Microsoft XML HTTP object for simple GET requests.
Sample PowerScript Code
integer li_rc oleobject loo_xmlhttp string ls_url, ls_response // Initialize the OLE object loo_xmlhttp = CREATE oleobject li_rc = loo_xmlhttp.ConnectToNewObject("MSXML2.XMLHTTP.6.0") IF li_rc <> 0 THEN MessageBox("Error", "Failed to create XMLHTTP object: " + String(li_rc)) DESTROY loo_xmlhttp RETURN END IF // Define your target API URL ls_url = "http://172.16.0.14:30201/childTable?groupId=SNS16495&dictCode=02001" // Open and send a synchronous GET request loo_xmlhttp.open("GET", ls_url, FALSE) loo_xmlhttp.send() // Handle the response IF loo_xmlhttp.status = 200 THEN ls_response = loo_xmlhttp.responseText // Here you can process the returned data (XML/JSON) MessageBox("Success", "Fetched data: " + ls_response) ELSE MessageBox("Error", "Request failed. Status code: " + String(loo_xmlhttp.status)) END IF // Clean up the object DESTROY loo_xmlhttp
Method 2: Use WinHttp.WinHttpRequest
Another robust option, the WinHTTP object offers more control over HTTP settings like proxies and authentication if your API requires them.
Sample PowerScript Code
integer li_rc oleobject loo_winhttp string ls_url, ls_response // Initialize the OLE object loo_winhttp = CREATE oleobject li_rc = loo_winhttp.ConnectToNewObject("WinHttp.WinHttpRequest.5.1") IF li_rc <> 0 THEN MessageBox("Error", "Failed to create WinHttpRequest object: " + String(li_rc)) DESTROY loo_winhttp RETURN END IF // Define your target API URL ls_url = "http://172.16.0.14:30201/childTable?groupId=SNS16495&dictCode=02001" // Open and send a synchronous GET request loo_winhttp.open("GET", ls_url, FALSE) loo_winhttp.send() // Handle the response IF loo_winhttp.Status = 200 THEN ls_response = loo_winhttp.ResponseText // Process the data (you'll need to parse XML/JSON manually or with helper tools) MessageBox("Success", "Fetched data: " + ls_response) ELSE MessageBox("Error", "Request failed. Status code: " + String(loo_winhttp.Status)) END IF // Clean up the object DESTROY loo_winhttp
Key Notes for Implementation
- Data Parsing: PB 11.5 doesn't have native JSON support. If your API returns JSON, you can use the
Scripting.DictionaryOLE object to parse it, or use a third-party PowerBuilder JSON library like PBJSON. For XML responses, use PB's built-inXMLParserobject. - Synchronous vs Asynchronous: The examples use synchronous requests (third parameter set to
FALSE) for simplicity. If you need asynchronous requests, you'll have to set up callback functions, which is more complex in PB. - Authentication/Proxies: If the API requires authentication (like OAuth or basic auth), add a request header with
loo_xmlhttp.setRequestHeader("Authorization", "Bearer YOUR_TOKEN")or similar. For proxies, use theSetProxymethod on the WinHTTP object. - Component Dependencies: Ensure client machines have the required OLE components installed (MSXML 6.0 or WinHTTP 5.1 are pre-installed on most modern Windows systems).
内容的提问来源于stack exchange,提问作者user8645601
相关产品推荐
相关产品推荐

