SAP调第三方系统接口-直连:IF_HTTP_CLIENT

有些第三方系统创建的链接,参数我们无法通过PO连接,就直接采用直连的方式:IF_HTTP_CLIENT实现类
比如这种方式传参:

SAP调第三方系统接口-直连:IF_HTTP_CLIENT

 

此时就需要采用最简单直接的方式了:直连
1.首先查看外部系统API,输入传出参数结构。
(1)若输入结构不为JSON格式,可通过:request->set_form_field方法直接传参;
(2)若输入参数为JSON格式,可通过REQUEST->SET_CDATA设置入参,只不过这里的JSON是需要我们按照API自己去拼接的;
(3)若传入参数为URL后的,直接在URL后拼接就好了;
(4)传出参数也需要根据结构去定义,结构对应第三方系统的集合{ },表对应那边的数组[ ],字段类型为STRING;
参考实例:

DATA lv_url TYPE string.
DATA lv_http_client TYPE REF TO if_http_client.
DATA lv_jsonstr TYPE string.
DATA lv_token TYPE string.
DATA l_sysubrc LIKE sy-subrc.
DATA l_error_text TYPE string.
DATA lv_json TYPE string.
“字段名与外部系统保持一致
TYPES: BEGIN OF ty_dep,
originalids TYPE string,
leadertype TYPE string,
END OF ty_dep.
DATA:lt_dep TYPE TABLE OF ty_dep.
TYPES:BEGIN OF ty_org,
name TYPE string,
code TYPE string,
departmentleaders LIKE lt_dep,
END OF ty_org.
DATA:lt_org TYPE TABLE OF ty_org.
DATA:BEGIN OF gt_bsorg,
total TYPE string,
code TYPE string,
message TYPE string,
items LIKE lt_org, ”内表
END OF gt_bsorg.
“根据项目中的地址来赋值:若参数需赋值在URL中,可直接拼接
CONCATENATE 'https:*************** ”INTO lv_url.
*创建客户端请求
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
* proxy_host =
* proxy_service =
* ssl_id =
* sap_username =
* sap_client =
IMPORTING
client = lv_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
* RAISE createservicefailed.
ENDIF.

" 设置编码格式
* CALL METHOD lv_http_client->request->set_header_field
* EXPORTING
* name = 'Content-Type'
*VALUE = 'text/html;charset=utf-8'. "发送字符串时只能用utf-8编码/XML
** value = 'application/x-www-form-urlencoded'. "x-www-form
value = 'application/json;charset=utf-8'. "JSON 默认格式
" 若需要传TOKEN
CONCATENATE 'Bearer' ls_token INTO lv_token SEPARATED BY ' '.
* 设置token
CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = 'Authorization'
value = lv_token. "传Token
* 设置方法
CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'GET'. “POST GET.....
* 设置入参JSON
* CALL METHOD L_HTTP_CLIENT->REQUEST->SET_CDATA
* EXPORTING
* DATA = LV_JSON.
* 设置入参,一般传参
* CALL METHOD lv_http_client->request->set_form_field
* EXPORTING
* name = 'app_id' ”入参字段名-外部系统
* value = lv_appid.

* 发送请求
CALL METHOD lv_http_client->send
* EXPORTING
* timeout = CO_TIMEOUT_DEFAULT
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.

IF sy-subrc <> 0.
c_msg = '发送HTTP失败,检查连接'.
ENDIF.
IF c_msg IS INITIAL.
* 读取远程服务返回的处理过结果。
CALL METHOD lv_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.

IF sy-subrc <> 0.
c_msg = '读取HTTP失败,检查连接'.
l_sysubrc = sy-subrc.
* 返回错误连接文本
CALL METHOD lv_http_client->get_last_error
IMPORTING
code = l_sysubrc
message = l_error_text.
ENDIF.
* 获取数据 JSON格式
CALL METHOD lv_http_client->response->if_http_entity~get_cdata
* EXPORTING
* offset = 0
* length = -1
* virus_scan_profile = '/SIHTTP/HTTP_UPLOAD'
* vscan_scan_always = SPACE
RECEIVING
data = lv_jsonstr. ”返回JSON
*JSON -> ABAP
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_jsonstr
pretty_name = 'X'
CHANGING
data = gt_bsorg. "gt_bsorg为接收结构内表
ENDIF.
* 关闭
CALL METHOD lv_http_client->close( ).
“此时数据已经全部传输到gt_bsorg表中

注意:
若对方是HTTPS,需要将对方证书导入SAP中,否则我们无法访问,可让BASIS处理,对于HTTP的不需要关注
————————————————
版权声明:本文为CSDN博主「SAP.单刀赴会」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43734184/article/details/118993452

上一篇:PostgreSQL简单上手(windows环境)


下一篇:mqtt模拟