以下示例是ABAP通过调用http的方式来发送业务数据到其他系统 。
注意:ABAP<=>JSON 我使用了ZUI2_JSON这个类里面的方法。
FUNCTION ZOFSFM001.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IS_HEAD) TYPE ZOFSS001
*" EXPORTING
*" VALUE(EV_TYPE) TYPE CHAR1
*" VALUE(EV_MESS) TYPE BAPIRET2-MESSAGE
*" TABLES
*" IT_ITEM STRUCTURE ZOFSS002 OPTIONAL
*"----------------------------------------------------------------------
DATA: LS_SO TYPE ZOFSS005.
DATA: LT_SO TYPE TABLE OF ZOFSS005.
DATA: LT_ITEM TYPE ZOFSI001,
LS_ITEM TYPE ZOFSS002.
DATA: LS_T003 TYPE ZOFST003.
CONSTANTS:LC_URL TYPE STRING
VALUE 'http://192.168.197.21:8080/login'.
DATA: LO_UI2_JSON TYPE REF TO ZUI2_JSON.
DATA: LV_URL TYPE STRING.
DATA: LV_MESS_RETURN TYPE STRING.
DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT.
DATA: LO_CLIENT TYPE REF TO IF_HTTP_CLIENT.
DATA: LV_LEN TYPE I."发送报文长度
DATA: LV_RESPON TYPE STRING.
DATA: LV_SUBRC TYPE SY-SUBRC.
DATA: LV_JSON_STR TYPE STRING. "发送报文
DATA: LV_MESS TYPE STRING.
DATA: LV_STATU_RETURN TYPE C.
DATA: BEGIN OF LS_RES,
CODE TYPE STRING,
MSG TYPE STRING,
END OF LS_RES.
DATA: BEGIN OF LS_ID,
USERNAME TYPE STRING,
PASSWORD TYPE STRING,
END OF LS_ID.
DATA: BEGIN OF LS_RES_ID,
CODE TYPE STRING,
MSG TYPE STRING,
TOKEN TYPE STRING,
END OF LS_RES_ID.
CLEAR LV_MESS_RETURN.
CLEAR: LS_ITEM.
REFRESH LT_ITEM.
MOVE-CORRESPONDING IS_HEAD TO LS_SO.
MRC_DELETE_ZERO LS_SO-KUNNR LS_SO-KUNNR.
LOOP AT IT_ITEM.
MOVE-CORRESPONDING IT_ITEM TO LS_ITEM.
MRC_DELETE_ZERO LS_ITEM-MATNR LS_ITEM-MATNR.
MRC_DELETE_ZERO LS_ITEM-PRCTR LS_ITEM-PRCTR.
APPEND LS_ITEM TO LT_ITEM.
CLEAR:IT_ITEM,LS_ITEM.
ENDLOOP.
LS_SO-CHILDREN[] = LT_ITEM[].
APPEND LS_SO TO LT_SO.
CLEAR LS_T003.
SELECT SINGLE *
FROM ZOFST003
INTO CORRESPONDING FIELDS OF LS_T003
WHERE ZFUNCT = 'ZOFSFM001'.
CREATE OBJECT LO_UI2_JSON.
IF LS_T003-ZURL IS NOT INITIAL.
LV_URL = LS_T003-ZURL.
IF LT_SO[] IS NOT INITIAL.
"创建客户端请求
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LC_URL
IMPORTING
CLIENT = LO_HTTP_CLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
MSGID = SY-MSGID
MSGNR = SY-MSGNO
MSGV1 = SY-MSGV1
MSGV2 = SY-MSGV2
MSGV3 = SY-MSGV3
MSGV4 = SY-MSGV4
IMPORTING
MESSAGE_TEXT_OUTPUT = LV_MESS_RETURN.
ENDIF.
LS_ID-USERNAME = 'sap'.
LS_ID-PASSWORD = '123456'.
IF LV_MESS_RETURN IS INITIAL.
CLEAR LV_JSON_STR.
CALL METHOD ZUI2_JSON=>SERIALIZE
EXPORTING
DATA = LS_ID
RECEIVING
R_JSON = LV_JSON_STR.
REPLACE 'USERNAME' WITH 'username' INTO LV_JSON_STR.
REPLACE 'PASSWORD' WITH 'password' INTO LV_JSON_STR.
LO_HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = LO_HTTP_CLIENT->CO_DISABLED.
"设定传输请求内容格式以及编码格式
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( LV_JSON_STR ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_JSON_STR
OFFSET = 0
LENGTH = LV_LEN.
"发送请求
LO_HTTP_CLIENT->SEND( EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2 ).
IF SY-SUBRC <> 0.
"操作失败,获取失败原因
LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = LV_MESS ).
LV_MESS_RETURN = LV_MESS.
LV_STATU_RETURN = 'E'.
CLEAR LV_MESS.
ENDIF.
"读取远程服务返回的处理过结果。
LO_HTTP_CLIENT->RECEIVE( EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3 ).
IF SY-SUBRC <> 0 .
LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = LV_MESS ).
LV_MESS_RETURN = LV_MESS.
LV_STATU_RETURN = 'E'.
CLEAR LV_MESS.
ELSE.
"读取返回返回内容
CLEAR LV_RESPON.
CLEAR LS_RES_ID.
LV_RESPON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
CALL METHOD ZUI2_JSON=>DESERIALIZE
EXPORTING
JSON = LV_RESPON
CHANGING
DATA = LS_RES_ID.
LV_MESS_RETURN = LS_RES_ID-MSG.
IF LS_RES_ID-CODE = '200'.
LV_STATU_RETURN = 'S'.
ELSE.
LV_STATU_RETURN = 'E'.
ENDIF.
ENDIF.
CALL METHOD LO_HTTP_CLIENT->CLOSE.
EV_TYPE = LV_STATU_RETURN.
EV_MESS = LV_MESS_RETURN.
ENDIF.
CHECK EV_TYPE NE 'E'.
CLEAR:LV_STATU_RETURN,LV_MESS_RETURN,EV_TYPE,EV_MESS.
"创建客户端请求
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 NE 0.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
MSGID = SY-MSGID
MSGNR = SY-MSGNO
MSGV1 = SY-MSGV1
MSGV2 = SY-MSGV2
MSGV3 = SY-MSGV3
MSGV4 = SY-MSGV4
IMPORTING
MESSAGE_TEXT_OUTPUT = LV_MESS_RETURN.
ENDIF.
IF LV_MESS_RETURN IS INITIAL.
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'Authorization'
VALUE = LS_RES_ID-TOKEN.
CLEAR LV_JSON_STR.
CREATE OBJECT LO_UI2_JSON.
CALL METHOD LO_UI2_JSON->SERIALIZE_INT
EXPORTING
DATA = LT_SO
RECEIVING
R_JSON = LV_JSON_STR.
LO_HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = LO_HTTP_CLIENT->CO_DISABLED.
"设定传输请求内容格式以及编码格式
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( LV_JSON_STR ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_JSON_STR
OFFSET = 0
LENGTH = LV_LEN.
"发送请求
LO_HTTP_CLIENT->SEND( EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2 ).
IF SY-SUBRC <> 0.
"操作失败,获取失败原因
LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = LV_MESS ).
LV_MESS_RETURN = LV_MESS.
LV_STATU_RETURN = 'E'.
CLEAR LV_MESS.
ENDIF.
"读取远程服务返回的处理过结果。
LO_HTTP_CLIENT->RECEIVE( EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3 ).
IF SY-SUBRC <> 0 .
LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = LV_MESS ).
LV_MESS_RETURN = LV_MESS.
LV_STATU_RETURN = 'E'.
CLEAR LV_MESS.
ELSE.
"读取返回返回内容
CLEAR LV_RESPON.
CLEAR LS_RES.
LV_RESPON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
CALL METHOD ZUI2_JSON=>DESERIALIZE
EXPORTING
JSON = LV_RESPON
CHANGING
DATA = LS_RES.
LV_MESS_RETURN = LS_RES-MSG.
IF LS_RES-CODE = '200'.
LV_STATU_RETURN = 'S'.
ELSE.
LV_STATU_RETURN = 'E'.
ENDIF.
ENDIF.
CALL METHOD LO_HTTP_CLIENT->CLOSE.
EV_TYPE = LV_STATU_RETURN.
EV_MESS = LV_MESS_RETURN.
ELSE.
EV_TYPE = 'E'.
EV_MESS = LV_MESS_RETURN.
ENDIF.
ELSE.
EV_TYPE = 'E'.
EV_MESS = '请先配置接口发送地址'.
ENDIF.
ENDIF.
ENDFUNCTION.