ABAP访问http请求

POST请求


  METHOD GET_TOKEN.

    TYPES: BEGIN OF lty_user,
             login_name      TYPE string,
             i_p_address     TYPE string,
             integration_key TYPE string,
           END OF lty_user,
           BEGIN OF lty_token,
             data            TYPE string,
             datadescription TYPE string,
             result          TYPE string,
             message         TYPE string,
           END OF lty_token..

    DATA: lo_http_client TYPE REF TO if_http_client,
          lv_service     TYPE string,
          lv_name        TYPE rvari_vnam,
          lv_result      TYPE string,
          ls_usr         TYPE lty_user,
          lv_json        TYPE string,
          lv_len         TYPE i,
          ls_token       TYPE lty_token,
          lv_errocode    TYPE sysubrc,
          lv_terminal    TYPE xuterminal,
          lv_ip          TYPE xuterminal,
          lv_pcname      TYPE xuterminal,
          lv_message     TYPE string.

    IF ( IV_TYPE = 'P' ) AND mv_token IS NOT INITIAL.
      rv_token = mv_token.
      RETURN.
    ENDIF.

    lv_name = 'ZRRE_FILE_IP'.
    DATA(lv_file_ip) = get_configuration( lv_name ).
    lv_service = lv_file_ip &&'/api/services/Org/UserLoginIntegrationByUserLoginName'.
    cl_http_client=>create_by_url(
        EXPORTING
          url                = lv_service
        IMPORTING
          client             = lo_http_client
        EXCEPTIONS
          argument_not_found = 1
          plugin_not_active  = 2
          internal_error     = 3
          OTHERS             = 4 ).

    lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
    lo_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
    ls_usr-login_name = get_file_user( iv_type ).
    CALL FUNCTION 'TERMINAL_ID_GET'
      EXPORTING
        username             = sy-uname
      IMPORTING
        terminal             = lv_terminal
      EXCEPTIONS
        multiple_terminal_id = 1
        no_terminal_found    = 2
        OTHERS               = 3.
    IF sy-subrc = 0.
      SPLIT lv_terminal AT '-' INTO lv_ip lv_pcname.
    ENDIF.
    IF lv_ip IS INITIAL.
      lv_ip = '127.0.0.1'.
    ENDIF.
    ls_usr-i_p_address = lv_ip.
    lv_name = 'ZRRE_INTERGRATION_KEY'.
    DATA(lv_integration_key) = get_configuration( lv_name ).
    ls_usr-integration_key = lv_integration_key.

    lv_json = /ui2/cl_json=>serialize( data = ls_usr pretty_name = /ui2/cl_json=>pretty_mode-extended ).
    lv_len = strlen( lv_json ) .

    CALL METHOD lo_http_client->request->set_cdata
      EXPORTING
        data   = lv_json
        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 ).

*  *  接收返回消息
    CALL METHOD lo_http_client->get_last_error
      IMPORTING
        code    = lv_errocode
        message = lv_message.

    CLEAR lv_result .
    lv_result = lo_http_client->response->get_cdata( ).

*JSON->内表
    /ui2/cl_json=>deserialize( EXPORTING json = lv_result
                               CHANGING  data = ls_token ).
    "关闭接口
    CALL METHOD lo_http_client->close.
    rv_token = ls_token-data.
    mv_token = ls_token-data.

  ENDMETHOD.

GET请求

  method GET_ECM_USR_ID.

    DATA: lv_erp_uid TYPE string.

    lv_erp_uid = IV_ERP_UID.
    IF lv_erp_uid IS INITIAL.
      lv_erp_uid = sy-uname.
    ENDIF.

    TYPES: BEGIN OF lty_user,
             login_name      TYPE string,
             i_p_address     TYPE string,
             integration_key TYPE string,
           END OF lty_user,
           BEGIN OF lty_token,
             data            TYPE string,
             datadescription TYPE string,
             result          TYPE string,
             message         TYPE string,
           END OF lty_token..

    TYPES: BEGIN OF lty_top_position_info_data,
             ID               TYPE string,
             IdentityId       TYPE string,
             Code             TYPE string,
             ThirdPartId      TYPE string,
           END OF  lty_top_position_info_data.
    TYPES: BEGIN OF lty_top_position_info,
             data             TYPE lty_top_position_info_data,
             dataDescriptiont TYPE string,
             result           TYPE string,
             message          TYPE string,
           END OF lty_top_position_info.

    DATA: lo_http_client TYPE REF TO if_http_client,
          lv_service     TYPE string,
          lv_name        TYPE rvari_vnam,
          lv_result      TYPE string,
          ls_usr         TYPE lty_user,
          lv_json        TYPE string,
          lv_len         TYPE i,
          ls_token       TYPE lty_token,
          ls_pos_info       TYPE lty_top_position_info,
          lv_errocode    TYPE sysubrc,
          lv_terminal    TYPE xuterminal,
          lv_ip          TYPE xuterminal,
          lv_pcname      TYPE xuterminal,
          lv_message     TYPE string.


*    get_token( ).
    lv_name = 'ZRRE_FILE_IP'.
    DATA(lv_file_ip) = get_configuration( lv_name ).
    ls_usr-login_name = get_file_user(  ).
    lv_service = lv_file_ip &&'/api/services/OrgUser/GetUserInfoByAccount?token=' && get_token_lasting( IV_TYPE = 'U' ) && '&account=' && lv_erp_uid.
    cl_http_client=>create_by_url(
        EXPORTING
          url                = lv_service
        IMPORTING
          client             = lo_http_client
        EXCEPTIONS
          argument_not_found = 1
          plugin_not_active  = 2
          internal_error     = 3
          OTHERS             = 4 ).

    lo_http_client->request->set_method( if_http_request=>co_request_method_get ).
    lo_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
    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 ).

*  *  接收返回消息
    CALL METHOD lo_http_client->get_last_error
      IMPORTING
        code    = lv_errocode
        message = lv_message.

    CLEAR lv_result .
    lv_result = lo_http_client->response->get_cdata( ).

*JSON->内表
    /ui2/cl_json=>deserialize( EXPORTING json = lv_result
                               CHANGING  data = ls_pos_info ).
    "关闭接口
    CALL METHOD lo_http_client->close.
    IF ls_pos_info-result = 0.
      rv_usr_id = ls_pos_info-data-id.
      SELECT SINGLE NAME_TEXT INTO @DATA(lv_uname)  FROM ZV_USERNAME WHERE BNAME = @IV_ERP_UID.

      DATA: ls_ecm_acc TYPE ZRRE_D_ECM_ACC.
      ls_ecm_acc-BNAME = IV_ERP_UID.
      ls_ecm_acc-ECMID = rv_usr_id.
      ls_ecm_acc-NAME_TEXTC = lv_uname.
      ls_ecm_acc-CDATE = sy-datum.
      UPDATE ZRRE_D_ECM_ACC SET ecmid = @rv_usr_id WHERE BNAME = @LV_ERP_UID.

    ENDIF.

  endmethod.