SAP HTTP调用其他系统接口

1业务说明

ABAP系统通过HTTP方式调用其他系统发布的接口

2代码实现

2.1认证接口

根据访问的URL创建HTTP客户端

SAP HTTP调用其他系统接口_第1张图片

设置访问方式,并调用SEND和接收函数

SAP HTTP调用其他系统接口_第2张图片

有时需要专门验证用户名密码

SAP HTTP调用其他系统接口_第3张图片

获取返回的HTTP访问状态和状态描述,并关闭接口

SAP HTTP调用其他系统接口_第4张图片

其中定义的接收内表需要按照JSON结构来定义

SAP HTTP调用其他系统接口_第5张图片

SAP HTTP调用其他系统接口_第6张图片

2.2具体接口

当获取TOKEN成功之后,就可以继续执行后续的某个业务功能接口了

仍然根据访问的接口URL创建HTTP客户端

SAP HTTP调用其他系统接口_第7张图片

设置传输格式,校验权限,访问方式

SAP HTTP调用其他系统接口_第8张图片

根据传入JSON,定义内表,并转换为JSON格式

SAP HTTP调用其他系统接口_第9张图片

SAP HTTP调用其他系统接口_第10张图片

SAP HTTP调用其他系统接口_第11张图片

同理,调用发送和接收函数,之后用相同方式获取返回的JSON数据,并将其转换为内表,做后续操作,方法和认证接口处理一致,不再赘述。

2.3补充

SAP HTTP调用其他系统接口_第12张图片

 当调用x-www-form-urlencoded类型的链接时,设置头格式

 将传输参数通过&拼接起来

SAP HTTP调用其他系统接口_第13张图片

 在POSTMAN中,调用时自动将参数进行urlencode转换,调用如果需要,可以如上使用方法转换

lv_time_64 = cl_http_utility=>escape_url( timestamp ).

2.4源代码

"-----------------------------------------@斌将军--------------------------------------------
"TOKEN的结构
TYPES: BEGIN OF TY_TOKEN,
         ACCESS_TOKEN  TYPE STRING,
         TOKEN_TYPE    TYPE STRING,
         REFRESH_TOKEN TYPE STRING,
         EXPIRES_IN    TYPE STRING,
         SCOPE         TYPE STRING,
       END OF TY_TOKEN.

"发送数据时的结构
DATA:BEGIN OF TY_DATILDATA OCCURS 0,
       DATEFROM TYPE STRING,
       DATETO   TYPE STRING,
       PAGE     TYPE STRING,
       SIZE     TYPE STRING,
     END OF TY_DATILDATA.

"编辑发送数据
DATA: BEGIN OF TY_SENDDATA,
        PAYLOAD LIKE TY_DATILDATA,
      END OF TY_SENDDATA.

"编辑接收数据
DATA: BEGIN OF TY_RECEIVEDATA,
        PAYLOAD LIKE TY_DATILDATA,
        DATA    LIKE TABLE OF TY_DATILDATA,
      END OF TY_RECEIVEDATA.

DATA:LS_TOKEN    TYPE TY_TOKEN,
     LS_SENDDATA LIKE TY_SENDDATA,
     LS_RECDATA  LIKE TY_RECEIVEDATA.

DATA: LV_LEN           TYPE I, "发送报文长度
      LV_URL           TYPE STRING, "接口地址
      LO_HTTP_CLIENT   TYPE REF TO IF_HTTP_CLIENT, "http客户端
      LV_SEND_JSON     TYPE STRING, "发送的JSON
      LV_REC_JSON      TYPE STRING, "接收的JSON
      LV_AUTHORIZATION TYPE STRING,
      LV_CODE          TYPE I, "HTTP 返回状态
      LV_REASON        TYPE  STRING. " HTTP 状态描述

START-OF-SELECTION.

  CLEAR:LV_URL.
  LV_URL = 'https://hostname/oauth/oauth/token'.

  "创建http客户端
  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.

  "不显示登录界面
  LO_HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = LO_HTTP_CLIENT->CO_DISABLED.

  "可以使用AUTHENTICATE方法专门验证用户名和密码,但本程序不适用
*  DATA LV_USERNAME TYPE STRING.
*  DATA LV_PASSWORD TYPE STRING.
*  Lv_USERNAME = '1234567'.
*  Lv_PASSWORD = '1234567'.
*  CALL METHOD LO_HTTP_CLIENT->AUTHENTICATE
*    EXPORTING
*      USERNAME = Lv_USERNAME
*      PASSWORD = Lv_PASSWORD.

  "设置http method 为POST
  LO_HTTP_CLIENT->REQUEST->SET_METHOD(
    IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
  ).
  "IF_HTTP_ENTITY~CO_REQUEST_METHOD_GET

  "发送
  CALL METHOD LO_HTTP_CLIENT->SEND
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3
      HTTP_INVALID_TIMEOUT       = 4
      OTHERS                     = 5.

  "接收
  CALL METHOD LO_HTTP_CLIENT->RECEIVE
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3.

  "提取返回字符串
  CLEAR:LV_CODE,LV_REASON.
  CALL METHOD LO_HTTP_CLIENT->RESPONSE->GET_STATUS
    IMPORTING
      CODE   = LV_CODE
      REASON = LV_REASON.

  "获取返回的JSON
  CLEAR:LV_REC_JSON,LS_TOKEN.
  LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
  /UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
                              CHANGING  DATA = LS_TOKEN ).

  "关闭接口
  CALL METHOD LO_HTTP_CLIENT->CLOSE.

  "获取Token
  CLEAR:LV_AUTHORIZATION.
  LV_AUTHORIZATION = LS_TOKEN-ACCESS_TOKEN.

  "如果有值,证明返回了正确的TOKEN,就可以继续
  IF LS_TOKEN IS NOT INITIAL AND LV_CODE = '200'.
    CLEAR:LV_URL.
    LV_URL = 'https:// hostname/ssrc/v1/448/rgwd/price-library/list'.
    "创建http客户端
    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.

    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'
    ).

    CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
      EXPORTING
        NAME  = 'Authorization'
        VALUE = LV_AUTHORIZATION.

    "设置http method 为POST
    LO_HTTP_CLIENT->REQUEST->SET_METHOD(
      IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
    ).
    "IF_HTTP_ENTITY~CO_REQUEST_METHOD_GET
    CLEAR:LS_SENDDATA.
    LS_SENDDATA-PAYLOAD-DATEFROM = '20191231 00:00:00'.
    LS_SENDDATA-PAYLOAD-DATETO = '20191231 00:00:00'.
    LS_SENDDATA-PAYLOAD-PAGE = 0.
    LS_SENDDATA-PAYLOAD-SIZE = 50.
    "SAP不区分大小写,JAVA/C会区分,
    "一定让对方修改代码,不然只能SBB的拼接
    CLEAR:LV_SEND_JSON.
    LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( LS_SENDDATA ).
    "设置待传输内容长度
    CLEAR:LV_LEN.
    LV_LEN = STRLEN( LV_SEND_JSON ).

    CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
      EXPORTING
        DATA   = LV_SEND_JSON
        OFFSET = 0
        LENGTH = LV_LEN.

    "发送
    CALL METHOD LO_HTTP_CLIENT->SEND
      EXCEPTIONS
        HTTP_COMMUNICATION_FAILURE = 1
        HTTP_INVALID_STATE         = 2
        HTTP_PROCESSING_FAILED     = 3
        HTTP_INVALID_TIMEOUT       = 4
        OTHERS                     = 5.

    "接收
    CALL METHOD LO_HTTP_CLIENT->RECEIVE
      EXCEPTIONS
        HTTP_COMMUNICATION_FAILURE = 1
        HTTP_INVALID_STATE         = 2
        HTTP_PROCESSING_FAILED     = 3.

    CLEAR:LV_REC_JSON.",LS_JSON.
    LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
    "解析JSON串
    CLEAR:LS_RECDATA.
    /UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
          PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
                               CHANGING DATA = LS_RECDATA ).

    "关闭接口
    CALL METHOD LO_HTTP_CLIENT->CLOSE.
    IF LS_RECDATA IS NOT INITIAL.
      "将获取到的数据做其他的操作
    ELSE.
      "报错
    ENDIF.

  ELSEIF LV_CODE <> '200'.
    WRITE:LV_CODE.
    WRITE:/ LV_REASON.
  ENDIF.
"-----------------------------------------@斌将军--------------------------------------------

你可能感兴趣的:(ABAP,json,http,abap)