ABAP中实现http client

来源:互联网 发布:p2p网络借贷监管 编辑:程序博客网 时间:2024/05/22 12:56

现在SAP全面拥抱web和云,了解一下http的实现还是有必要的。ABAP中主要通过接口if_http_client来实现http的send, receive功能


REPORT  zhttp_client NO STANDARD PAGE HEADING.

DATAlo_http_client TYPE REF TO if_http_client,
      lv_uri         TYPE string,
      lv_results     TYPE string.
DATAitab TYPE TABLE OF string.
DATAp_code TYPE i,
      p_reason TYPE string.
*   Create an instance of the HTTP client:
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                'http://yourwebsite/index.html'
  IMPORTING
    client             lo_http_client
  EXCEPTIONS
    argument_not_found 1
    plugin_not_active  2
    internal_error     3
    OTHERS             4.

*   Select the HTTP GET method:
lo_http_client->request->set_method(
  if_http_request=>co_request_method_get ).

*   Submit the request:
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.

** 得到response,这一步是必须的
CALL METHOD lo_http_client->receive
  EXCEPTIONS
    http_communication_failure 1
    http_invalid_state         2
    http_processing_failed     3.

IF sy-subrc EQ 0.
  lv_results =
    lo_http_client->response->get_cdata).


  CALL METHOD lo_http_client->response->get_status
    IMPORTING
      code   p_code
      reason p_reason.
ELSE.
*      RAISE EXCEPTION TYPE lcx_icf_exception.
ENDIF.

SPLIT lv_results AT cl_abap_char_utilities=>cr_lf INTO TABLE itab.  "把html内容放到内表

WRITE'Status:',p_codep_reason.
ULINE.
LOOP AT itab INTO lv_results.
  WRITE/ lv_results.
ENDLOOP.

*   Always remember to close the connection:
lo_http_client->close).

原创粉丝点击