使用powerbuilder调用restfull服务

时间:2015-08-04 07:49:50

标签: powerbuilder

是否可以在powerbuilder 10.0中调用restfull服务?

2 个答案:

答案 0 :(得分:0)

您可以使用两个Windows COM对象Msxml2.DOMDocumentMSXML2.ServerXMLHTTP来执行此操作。作为演示,此代码段调用返回当前日期和时间的REST服务。此服务托管在http://www.jsontest.com/

OleObject lole_Send
OleObject lole_SrvHTTP
OleObject lole_ScriptControl

String ls_status
String ls_response
String ls_value

lole_Send = CREATE OleObject
lole_SrvHTTP = CREATE OleObject
lole_Send.connectToNewObject("Msxml2.DOMDocument.6.0")
lole_SrvHTTP.connectToNewObject("MSXML2.ServerXMLHTTP.6.0")
lole_SrvHTTP.Open("GET", "http://date.jsontest.com", FALSE)
lole_SrvHTTP.SetRequestHeader( "Content-Type", "application/json")
lole_SrvHTTP.Send(lole_Send)
ls_status = string(lole_SrvHTTP.Status)
ls_response = string(lole_SrvHTTP.ResponseText)

MessageBox("REST HTTP response", ls_status)  // 200 is OK!
MessageBox("JSON response", ls_response)

lole_ScriptControl = CREATE OleObject
lole_ScriptControl.ConnectToNewObject( "MSScriptControl.ScriptControl" )
lole_ScriptControl.Language = "JScript"
lole_ScriptControl.AddCode("function getValue(s,key) {eval(~"jsonobj=~" + s); return eval(~"jsonobj.~" + key) ; }")

TRY
  // remove (replace with "") all the carriage return to clean up the JSON string
  int position, i
  FOR i = 1 to len(ls_response)
        position = pos(ls_response, "~n")
        IF position > 0 THEN
            ls_response= Replace(ls_response, position, 1, "")
        END IF
  NEXT
  MessageBox("JSON response cleanup", ls_response)
  // typical response
  //   { "time": "04:21:52 PM",  "milliseconds_since_epoch": 1436113312190, "date": "07-05-2015" }

   ls_value =  lole_ScriptControl.Eval ("getValue(' " + ls_response + " ',  'date');")
   MessageBox("date value", ls_value)
CATCH ( Throwable e )
   MessageBox("Err", e.GetMessage())
END TRY

Source

答案 1 :(得分:0)

您可以使用以下方法调用restful service:

Long ll_result
String ls_url

OLEObject OLE_url 

OLE_url = <rest service URL>
CREATE OLEObject

OLE_url.ConnectToNewObject("WinHttp.WinHttpRequest.5.1")
OLE_url.OPEN("GET", ls_url, FALSE)

OLE_url.Send()
ll_result = Long(OLE_url.ResponseText()) //To get reply from rest service, if any