最新消息:本站持续更新中,请注意添加收藏夹。搜索关键词时,多换一个同义词。比如要搜索界面,可以尝试页面,画面,PER档等词汇。善于搜索,将大大提高你的查找效率。

T100——关于调用外部接口的方法记录

后端代码 bron1984 3726浏览

IMPORT util
IMPORT xml
IMPORT com

 

GET (获取)/ DELETE(删除):

PUBLIC FUNCTION ccl_webservice_get(p_url)
  DEFINE l_http_req      com.HTTPRequest
  DEFINE l_http_res      com.HTTPResponse
  DEFINE p_url           STRING
  DEFINE l_succ          BOOLEAN
  DEFINE l_str           STRING
  DEFINE l_body          STRING
  DEFINE p_method        LIKE type_t.chr100
  
  
  # initial parameter
  LET l_succ = FALSE
  LET l_str = ""
  
  TRY 
     # 設置 HttpRequest START
     LET  l_http_req = com.HttpRequest.Create(p_url)  # 建立請求 requesst 物件
     CALL l_http_req.setTimeOut(60)  # 設定 request timeout     
     CALL l_http_req.setMethod("GET")  # 設定Http method
     CALL l_http_req.setCharset("UTF-8")  # 設定編碼
     CALL l_http_req.doRequest()  # 設定Http method

     LET  l_http_res = l_http_req.getResponse()  #取得回傳 response 物件
     IF l_http_res.getStatusCode() != 200 THEN
       # server 回傳報錯
       LET l_succ = FALSE
       LET l_str = "HTTP Error ("||l_http_res.getStatusCode()||") ", l_http_res.getStatusDescription()
     ELSE
       # 取得 server 回傳 body content string
       LET l_succ = TRUE
       LET  l_str = l_http_res.getTextResponse() # 取得 response body字串
     END IF
  CATCH
     #get error from genero
     LET l_succ = FALSE
     LET l_str = "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")"
  END TRY

  # 回傳結果
  RETURN l_succ,l_str
  
END FUNCTION

 

POST(新增)/PUT(修改):

1—参数为URL(带参?a=a&b=b),JSON(在requestBody里),此时用doTextRequest传递JSON;

2—参数为URL(不带参),URL参数(a=a&b=b),此时用doFormEncodedRequest传递URL参数;

 

1:

PUBLIC FUNCTION ccl_webservice_post(p_url,p_str)
  DEFINE l_http_req      com.HTTPRequest
  DEFINE l_http_res      com.HTTPResponse
  DEFINE p_url           STRING         #URL(可带参,不带json)
  DEFINE l_succ          BOOLEAN
  DEFINE l_str           STRING
  DEFINE l_body          STRING
  DEFINE p_str           STRING         #JSON数据,放requestbody里
  
  
  # initial parameter
  LET l_succ = FALSE
  LET l_str = ""
  
  TRY 
     # 設置 HttpRequest START
     LET  l_http_req = com.HttpRequest.Create(p_url)  # 建立請求 requesst 物件
     CALL l_http_req.setTimeOut(60)  # 設定 request timeout     
     CALL l_http_req.setMethod("POST")
     CALL l_http_req.setCharset("UTF-8")  # 設定編碼
     CALL l_http_req.setHeader("Content-Type", "application/json")        
     CALL l_http_req.doTextRequest(p_str)

     LET  l_http_res = l_http_req.getResponse()  #取得回傳 response 物件
     IF l_http_res.getStatusCode() != 200 THEN
       # server 回傳報錯
       LET l_succ = FALSE
       LET l_str = "HTTP Error ("||l_http_res.getStatusCode()||") ", l_http_res.getStatusDescription()
     ELSE
       # 取得 server 回傳 body content string
       LET l_succ = TRUE
       LET  l_str = l_http_res.getTextResponse() # 取得 response body字串
     END IF
  CATCH
     #get error from genero
     LET l_succ = FALSE
     LET l_str = "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")"
  END TRY

  # 回傳結果
  RETURN l_succ,l_str
  
END FUNCTION

2:

PUBLIC FUNCTION cs_t1client_http_post(p_url,p_str)
  DEFINE l_http_req      com.HTTPRequest
  DEFINE l_http_res      com.HTTPResponse
  DEFINE p_url           STRING
  DEFINE l_succ          BOOLEAN
  DEFINE l_str           STRING
  DEFINE l_body          STRING
  DEFINE p_str           STRING
  
  
  # initial parameter
  LET l_succ = FALSE
  LET l_str = ""
  
  TRY 
     # 設置 HttpRequest START
     LET  l_http_req = com.HttpRequest.Create(p_url)  # 建立請求 requesst 物件
     CALL l_http_req.setTimeOut(60)  # 設定 request timeout     
     CALL l_http_req.setMethod("POST")
     CALL l_http_req.setCharset("UTF-8")  # 設定編碼
     DISPLAY l_http_req
     CALL l_http_req.doFormEncodedRequest(p_str,true)  # send request
     
     LET  l_http_res = l_http_req.getResponse()  #取得回傳 response 物件
     IF l_http_res.getStatusCode() != 200 THEN
       # server 回傳報錯
       LET l_succ = FALSE
       LET l_str = "HTTP Error ("||l_http_res.getStatusCode()||") ", l_http_res.getStatusDescription()
     ELSE
       # 取得 server 回傳 body content string
       LET l_succ = TRUE
       LET  l_str = l_http_res.getTextResponse() # 取得 response body字串
       #DISPLAY l_str

     END IF
  CATCH
     #get error from genero
     LET l_succ = FALSE
     LET l_str = "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")"
  END TRY


  # 回傳結果
  RETURN l_succ,l_str
END FUNCTION

 

转载请注明:赫非域 » T100——关于调用外部接口的方法记录