SAP_ABAP_接口技术_API实践总结

1.1 代码模版

FUNCTION zfm_for_api.
*"----------------------------------------------------------------------
*"*"本地接口:
*"----------------------------------------------------------------------

  DATA:
    l_value        TYPE string,
    lt_fields      TYPE tihttpnvp,
    lw_fields      TYPE ihttpnvp,
    lo_http_client TYPE REF TO if_http_client,
    l_url          TYPE string,
    l_result       TYPE string,
    l_json         TYPE string,
    lo_part        TYPE REF TO if_http_entity,
    l_len          TYPE i.

*   确定url
  l_url = 'http://xxx.xxx.xxx.xxx:8888/ekp/api/km-review/kmReviewRestService/addReview'.

  DATA:lw_form     TYPE ihttpnvp.
  DATA:lt_form     TYPE tihttpnvp.
  DATA:re_response TYPE string.

*   创建http对象
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = l_url
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.

*   设定method
  CALL METHOD lo_http_client->request->set_method( 'POST' ).

  lo_http_client->request->set_form_fields( fields = lt_form ).
*   发送数据
  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 ).

  re_response  = l_result = lo_http_client->response->get_cdata( ).



ENDFUNCTION.

你可能感兴趣的:(SAP,Abap实战篇,经验分享)