SAP-ABAP调用外部接口HTTPS/HTTP 标准案列


DATA: LEN         TYPE        I, "发送报文长度
      LEN_STRING  TYPE        STRING,
      COUNT       TYPE        CHAR15,
      URL         TYPE        STRING, "接口地址
      URL_TOKEN   TYPE        STRING, "获取token地址
      HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, "http客户端
      HTTP_TOKEN  TYPE REF TO IF_HTTP_CLIENT, "http客户端
      POST_STRING TYPE        STRING,
      RESULT      TYPE        STRING,
      LV_RESULT   TYPE        STRING,
      LV_TOKEN    TYPE        STRING,
      LV_HEADER   TYPE STRING.
DATA: LV_JSON3 TYPE STRING,
      LV_JSON  TYPE String,
      LT_RES1  TYPE String,
      LT_ANDON TYPE String.

DATA:
  SERIALIZER TYPE REF TO CL_TREX_JSON_SERIALIZER,
  LV_JSON1    TYPE STRING.

URL = 'http:/XXXXXXXX'.

"创建http客户端
"创建http客户端
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
  EXPORTING
    URL                = URL
  IMPORTING
    CLIENT             = HTTP_CLIENT
  EXCEPTIONS
    ARGUMENT_NOT_FOUND = 1
    PLUGIN_NOT_ACTIVE  = 2
    INTERNAL_ERROR     = 3
    OTHERS             = 4.


"设定传输请求内容格式以及编码格式
HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' ).
HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).


"设置调用方法
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
  EXPORTING
    NAME  = '~request_method'
    VALUE = 'POST'.

CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
  EXPORTING
    NAME  = 'request_protocol'    "https请求则改为'~request_protocol'
    VALUE = 'HTTPS/1.1'.

*--------------------------------------------------------------------------------------------------------
CLEAR: LV_JSON3.

* 内表转换json
*CALL METHOD /UI2/CL_JSON=>SERIALIZE
*  EXPORTING
*    DATA   = LT_ANDON
*  RECEIVING
*    R_JSON = LV_JSON3.
LV_JSON3 = '{ "sapNo" : "ZCSHA009896" }'.
LV_JSON =  LV_JSON3.

"设置待传输内容长度
LEN = STRLEN( LV_JSON ).

"设置传入字符串
CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
  EXPORTING
    DATA   = LV_JSON
    OFFSET = 0
    LENGTH = LEN.

"发送
CALL METHOD 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.
  "操作失败,获取失败原因
  " HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = -MSGTX ).
  "-MSGTY = 'E'.

  "发送
  CALL METHOD 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.
    " -MSGTY = 'S'.
  ENDIF.
ELSE.
  " -MSGTY = 'S'.
  "-MSGTX = '发送成功'.
  RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
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 = -MSGTX1 ).
  "-MSGTY1 = 'E'.
ELSE.
  "-MSGTY1 = 'S'.
  "-MSGTX1 = '接收成功'.
ENDIF.

CLEAR RESULT.
RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( )."提取返回字符串
"将字符串中的回车符替换,否则abap将会识别为#
"REPLACE ALL OCCURRENCES OF REGEX '\n' IN result WITH space.
"-RES = RESULT.
WRITE :RESULT.
BREAK-POINT.

SAP-ABAP调用外部接口HTTPS/HTTP 标准案列_第1张图片

 注意:无需任何配置

你可能感兴趣的:(ABAP,ALV,http,https)