FUNCTION z_common_send_to_wechat.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(OS_IN) TYPE ZSCOMMON_WECHAT_IN
*"----------------------------------------------------------------------
DATA: lo_http_client TYPE REF TO if_http_client,
gv_json_out TYPE string,
lv_url TYPE string,
lv_head TYPE string,
lv_len TYPE i,
iv_json_input TYPE string,
ev_json_output TYPE string,
l_content TYPE char1024,
l_value TYPE savwctxt-fieldcont.
MOVE os_in-content TO l_value.
CALL FUNCTION 'WWW_URLENCODE'
EXPORTING
value = l_value
IMPORTING
value_encoded = l_content.
*
* BREAK-POINT.
lv_url = 'http://oa.luxshare-ict.com/wxinterface/push_operation_kp.jsp?workcodes='
&& os_in-receiver && '&msgtype=text&pass=luxshare888..&content=' && l_content.
"创建客户端请求
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.
"lv_subrc = sy-subrc.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
* sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
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_len = strlen( iv_json_input ).
"设置调用方法
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'POST'.
"设置传入字符串
CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = iv_json_input
offset = 0
length = lv_len.
"发送数据
lo_http_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ).
"接收返回
lo_http_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
"提取返回字符串
ev_json_output = lo_http_client->response->get_cdata( ).
"将字符串中的回车符替换,否则abap将会识别为#
REPLACE ALL OCCURRENCES OF REGEX '/n' IN ev_json_output WITH space.
ENDFUNCTION.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19541.html