参考例子如下:
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.