FUNCTION ztools_call_http .
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(IP_URL) TYPE STRING
*" REFERENCE(IP_BODY) TYPE STRING OPTIONAL
*" REFERENCE(IP_METHOD) TYPE STRING DEFAULT 'POST'
*" EXPORTING
*" REFERENCE(V_SUBRC) TYPE CHAR1
*" REFERENCE(V_MESSAGE) TYPE BAPI_MSG
*" REFERENCE(V_RESPON) TYPE STRING
*" REFERENCE(V_XRESPON) TYPE XSTRING
*"----------------------------------------------------------------------
DATA lv_message TYPE string.
CLEAR:lv_message,v_subrc,v_message,v_respon.
DATA http_client TYPE REF TO if_http_client.
DATA lv_url TYPE string.
lv_url = ip_url.
IF lv_url IS INITIAL.
v_subrc = 'E'.
v_message = 'URL链接为空'.
RETURN.
ENDIF.
"创建客户端请求
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
v_subrc = 'E'.
IF http_client IS INITIAL.
v_message = '创建http连接失败!'.
ELSE.
http_client->get_last_error( IMPORTING message = lv_message ).
CONCATENATE '创建http连接失败!' lv_message INTO v_message.
ENDIF.
RETURN.
ENDIF.
http_client->propertytype_logon_popup = http_client->co_disabled.
"设定调用服务
CALL METHOD http_client->request->set_method
EXPORTING
method = ip_method.
"设定传输请求内容格式以及编码格式
CALL METHOD http_client->request->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json; charset=utf-8'.
* CALL METHOD http_client->request->set_header_field
* EXPORTING
* name = 'Content-Type'
* value = 'application/JSON;charset=utf-8'.
IF ip_body IS NOT INITIAL.
DATA length TYPE i.
length = STRLEN( ip_body ).
CALL METHOD http_client->request->set_cdata
EXPORTING
data = ip_body
offset = 0
length = length.
ENDIF.
"发送请求
CALL METHOD http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.
IF sy-subrc <> 0.
http_client->get_last_error( IMPORTING message = lv_message ).
CONCATENATE '创建http连接失败!' lv_message INTO v_message.
v_subrc = 'E'.
RETURN.
ENDIF.
"读取远程服务返回的处理过结果。
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc <> 0 .
http_client->get_last_error( IMPORTING message = lv_message ).
CONCATENATE '获取返回消息失败!' lv_message INTO v_message.
v_subrc = 'E'.
RETURN.
ELSE.
"读取返回返回内容
v_subrc = 'S'.
v_message = '调用接口成功!'.
v_respon = http_client->response->get_cdata( ).
v_xrespon = http_client->response->get_data( ).
ENDIF.
ENDFUNCTION.
调用:下载URL
METHOD downloadurl.
DATA: lv_url TYPE string,
lv_method TYPE string,
lv_json TYPE string.
DATA:lv_subrc TYPE char1,
lv_message TYPE bapi_msg,
lv_respon TYPE string,
lv_xrespon TYPE xstring.
DATA ls_web TYPE ztweb_param.
SELECT SINGLE * INTO ls_web FROM ztweb_param WHERE sys = 'YQZL' AND fun = 'DOWNLOADU'.
lv_url = ls_web-url.
lv_method = ls_web-method.
DATA:BEGIN OF ls_data,
cust_id TYPE char10,"为财资系统十位集团客户号
download_no TYPE char30,"下载编号
trade_type TYPE char30,"交易类型 RECEIPT:回单
END OF ls_data.
DATA:BEGIN OF ls_body,
_data LIKE ls_data,
END OF ls_body.
DATA:BEGIN OF ls_res,
retcode TYPE char4,"返回代码
retmsg TYPE char512,"返回信息
downloadurl TYPE char512,"下载URL
transstate TYPE char10,"交易状态
transmsg TYPE char100,"交易信息
END OF ls_res.
DATA:BEGIN OF ls_respon,
data LIKE ls_res,
END OF ls_respon.
ls_data-download_no = download_no.
ls_data-cust_id = custid.
ls_data-trade_type = trade_type.
ls_body-_data = ls_data.
lv_json = zcl_json=>serialize( data = ls_body pretty_name = 'X' ).
CALL FUNCTION 'ZTOOLS_CALL_HTTP'
EXPORTING
ip_url = lv_url
ip_body = lv_json
ip_method = lv_method
IMPORTING
v_subrc = lv_subrc
v_message = lv_message
v_respon = lv_respon
v_xrespon = lv_xrespon.
IF lv_subrc = 'S'.
zcl_json=>deserialize( EXPORTING json = lv_respon CHANGING data = ls_respon ).
IF ls_respon-data-retcode EQ '0000'.
IF ls_respon-data-transstate EQ '0'.
exp_type = 'S'.
downloadurl = ls_respon-data-downloadurl.
exp_msg = '请求成功'.
ELSE.
exp_type = 'E'.
exp_msg = ls_respon-data-transmsg.
ENDIF.
ELSE.
exp_type = 'E'.
exp_msg = ls_respon-data-retmsg.
ENDIF.
ELSE.
exp_type = lv_subrc.
exp_msg = lv_message.
ENDIF.
ENDMETHOD.