You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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.Dictionary OLE object to parse it, or use a third-party PowerBuilder JSON library like PBJSON. For XML responses, use PB's built-in XMLParser object.
  • 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 the SetProxy method 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:38:34