四、通过if_http_client类实现HTTP POST

*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_URL) TYPE  STRING OPTIONAL   "URI
*"     VALUE(IV_CONTENT_TYPE) TYPE  STRING OPTIONAL    "*Content type, for example 'application/json'
*"     VALUE(IV_SET_CDATA) TYPE  STRING OPTIONAL    "HTTP Request body
*"     VALUE(AUTHORIZATION) TYPE  STRING OPTIONAL   "Authorization: Bearer <accessToken>
*"  EXPORTING
*"     VALUE(RESPONSE_BODY) TYPE  STRING   "Delivers the HTTP body of this entity as character data
*"     VALUE(DATA_LENGTH) TYPE  I            "Supplies the length in bytes of the HTTP body
*"     VALUE(HTTP_STATUS) TYPE  STRING   "HTTP Status Code&description
*"----------------------------------------------------------------------
"本函数通过if_http_client类实现HTTP POST,过程如下:
"1.创建连接
"2.发送请求
"3.接收返回
"4.关闭连接
data:lv_token type string.

DATA:
  lo_http_client TYPE REF TO if_http_client,
  lv_url         TYPE string,
  lv_response    TYPE string,
  lv_http_code   TYPE i,
  lv_http_reason TYPE string.

  lv_url = iv_url.
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = lv_url
*     proxy_host         =
*     proxy_service      =
*     ssl_id             =
*     sap_username       =
*     sap_client         =
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.

 CONCATENATE 'Bearer' AUTHORIZATION INTO lv_token SEPARATED BY space.

CALL METHOD  lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Authorization'
      value = lv_token.

  lo_http_client->request->set_method( 'POST' ).
  lo_http_client->request->set_content_type( iv_content_type ).
  lo_http_client->request->set_cdata(
    EXPORTING
      data   = iv_set_cdata
*      offset = 0
*      length = -1
      ).

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

  lv_response = lo_http_client->response->get_cdata( ).
  "Supplies the length in bytes of the HTTP body
  lo_http_client->response->if_http_entity~get_data_length(
  IMPORTING
    data_length = data_length ).
  "Gets the current HTTP status code
  lo_http_client->response->get_status(
     IMPORTING
       code   = lv_http_code
       reason = lv_http_reason ).
  http_status = lv_http_code && lv_http_reason.
  CONDENSE http_status NO-GAPS.

  response_body = lv_response.

  lo_http_client->close( ).

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值