SAP調用RestfulApi接口接收數據

由於準備要作一個關於調用外部接口的需求,因此本身先練習一下。api

程序說明:我已經在.net開發的系統裏提供一個api接口,如今在sap訪問這個接口,來接收數據。bash

這裏涉及Restful Api知識,之後再分享。微信

這是一個api地址:http://10.X.X.X:8081/api/test/gettestpost

首先根據這個url建立一個http客戶端url

call method cl_http_client=>create_by_url
    exporting
      url                = url
    importing
      client             = http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.

選擇一個HTTP GET METHOD.net

http_client->request->set_method( if_http_request=>co_request_method_get ).

發送和接收數據視頻

 "發送
  call method http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      http_invalid_timeout       = 4
      others                     = 5.

  "接收
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.   

最後就能夠獲取接收的數據了blog

  "獲取接口返回的數據
  result = http_client->response->get_cdata( ).

  write: result.

輸出結果是這樣的教程

跟外部系統返回的數據是一致的。接口

附上完整代碼

data: len         type        i,"發送報文長度
      len_string  type        string,
      url         type        string, "接口地址
      http_client type ref to if_http_client,"http客戶端
      post_string type        string,
      result      type        string.

data: it_header type tihttpnvp.



start-of-selection.

  url = 'http://10.X.X.X:8081/api/test/gettest'.


  "建立http客戶端
  call method cl_http_client=>create_by_url
    exporting
      url                = url
    importing
      client             = http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.

  "設置http method 爲Get
  http_client->request->set_method( if_http_request=>co_request_method_get ).

  "發送
  call method http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      http_invalid_timeout       = 4
      others                     = 5.

  "接收
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

  "獲取接口返回的數據
  result = http_client->response->get_cdata( ).

  write: result.

  

 

之後會繼續分享用POST方法發送數據到外部接口的例子。  

做者:明光爍亮
出處:http://www.cnblogs.com/hezhongxun/ 微信號:HEme922 關注可分享SAP ECC虛擬環境,以及開發或業務視頻教程 本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。

相關文章
相關標籤/搜索