SAP ABAP 发送http 请求实例参考(http post request)

本文展示的实例是通过http请求向指定API发送业务数据

 following Code:

  DATA: lv_url          TYPE string.     "http 服务接口地址
  DATA: lo_http_client  TYPE REF TO if_http_client.
  DATA: lv_len          TYPE i."发送报文长度
  DATA: lv_resp         TYPE string.
  DATA: lv_data         TYPE string.
  DATA: lv_message  TYPE string.
  DATA: lv_mtype    TYPE bapi_mtype.
  DATA: lv_code     TYPE sysubrc.
  TRY.

        " get pdf
        PERFORM get_pdf_datas USING   iv_edoc_guid
                              CHANGING lv_data .
        " set http url
        lv_url = 'https://v0347-iflmap.avtsbhf.eu1.hana.ondemand.com/http/thpdf'.

        "create http client
        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 <> 0.
          "lv_subrc = sy-subrc.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
                     sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          EXIT.
        ENDIF.
        lo_http_client->authenticate( username = 'username'
                                      password = 'password' ).
        " set content type and character set
        lo_http_client->request->set_content_type( content_type = 'application/json; charset=base64' ).

        lo_http_client->request->set_method( if_http_request=>co_request_method_post ).

        "set datas
        lv_len  = strlen( lv_data ).
        CALL METHOD lo_http_client->request->set_cdata
          EXPORTING
            data   = lv_data
            offset = 0
            length = lv_len.

        " send request
        lo_http_client->send(  EXCEPTIONS http_communication_failure = 1
                                          http_invalid_state         = 2 ).

        IF sy-subrc <> 0.
          "error
          lo_http_client->get_last_error( IMPORTING message = lv_message code = lv_code ).
          MESSAGE 'http error' TYPE 'E'.
          EXIT.
        ENDIF.

        lo_http_client->receive( EXCEPTIONS http_communication_failure = 1
                                            http_invalid_state         = 2
                                            http_processing_failed     = 3 ).

        IF sy-subrc <> 0 .
          " lv_subrc = sy-subrc.
*          lo_http_client->get_last_error( IMPORTING message = lv_message code = lv_code  ).
          MESSAGE 'http error' TYPE 'E'.
          EXIT.
        ELSE.
          CLEAR lv_resp.
          lv_resp = lo_http_client->response->get_cdata( ).
        ENDIF .

    CATCH cx_edocument INTO lx_edocument.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDTRY.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gong JX

多谢鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值