SAP调用外部http url发送接收json格式数据

参考例子如下:

TYPES: BEGIN OF ty_data,
         zbytnum   TYPE char30,
         zbytvalue TYPE char30,
         ztime     TYPE char30,
       END OF ty_data.
DATA: lv_url         TYPE string,
      lv_response    TYPE string,
      lo_http_client TYPE REF TO if_http_client,
      lt_data        TYPE TABLE OF ty_data,
      lv_json_str    TYPE string,
      lv_len         TYPE i.

"---http接口URL
lv_url = 'http://XXXtestServlet'.
"----创建客户端请求
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = lv_url
  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
             sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

"---设置传输请求内容的格式和编码格式
lo_http_client->request->set_content_type( content_type = 'application/json;charset=utf-8' ).
"---设置调用服务
lo_http_client->request->set_method( if_http_request=>co_request_method_post ).

"---lv_json_str存放json格式数据
***  CALL METHOD /ui2/cl_json=>serialize
***    EXPORTING
***      data            = 内表数据
***    receiving
***      r_json          = lv_json_str.

"---设置要传输内容
lv_len = strlen( lv_json_str ).
CALL METHOD lo_http_client->request->if_http_entity~set_cdata
  EXPORTING
    data   = lv_json_str
    length = lv_len.

"---发送请求
CALL METHOD lo_http_client->send
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    http_invalid_timeout       = 4
    OTHERS                     = 5.
IF sy-subrc <> 0.
  "---获取失败原因
****    CALL METHOD lo_http_client->get_last_error
****      IMPORTING
****        code           = lv_code
****        message        = lv_message.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
               sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

"---获取http处理结果
CALL METHOD lo_http_client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.
IF sy-subrc <> 0.
  "---获取失败原因
****    CALL METHOD lo_http_client->get_last_error
****      IMPORTING
****        code           = lv_code
****        message        = lv_message.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
               sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

  "---读取返回内容
lv_response = lo_http_client->response->get_cdata( ).

REPLACE ALL OCCURRENCES OF '位号' IN lv_response WITH 'ZBYTNUM'.
REPLACE ALL OCCURRENCES OF '值' IN lv_response WITH 'ZBYTVALUE'.
REPLACE ALL OCCURRENCES OF '时间' IN lv_response WITH 'ZTIME'.

CALL METHOD /ui2/cl_json=>deserialize
  EXPORTING
    json = lv_response
  CHANGING
    data = lt_data.
"---关闭http连接
CALL METHOD lo_http_client->close.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值