【ABAP】Creat a client-server demo to process a http request using SAP Web AS

1.            Client端程序

*&---------------------------------------------------------------------*

*& Report  ZMAC_TEST

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

 

REPORT  zmac_test.

 

DATA: client TYPE REF TO if_http_client,

      errortext TYPE string.                     "used for error handling.

 

CALL METHOD cl_http_client=>create

  EXPORTING

    host              = 'your host domain or ip'

    service           = 'your service port'

*     proxy_host        = proxy_host

*     proxy_service     = proxy_service

    sap_username      = sy-uname

    sap_client        = sy-mandt

  IMPORTING

    client            = client

  EXCEPTIONS

    argument_not_found = 1

    internal_error     = 2

    plugin_not_active  = 3

    OTHERS             = 4.

 

 

* set http method GET

CALL METHOD client->request->set_method(

  if_http_request=>co_request_method_get ).

 

 

* set request uri (/<path>[?<querystring>])

client->request->set_cdata( '<html><body>ZJJMAC</body></html>' ).

client->request->set_form_field(

                 name     =     'data'

                 value    =     'adobe_form' ).

 

cl_http_utility=>set_request_uri( request = client->request

                                   uri    = '/sap/bc/zadobe_req' ).

*uri是你的服务所在路径

 

* Send

DATA timeout TYPE i.

CALL METHOD client->send

  EXPORTING

    timeout                    = timeout

  EXCEPTIONS

    http_communication_failure = 1

    http_invalid_state         = 2

    http_processing_failed     = 3

    OTHERS                     = 4.

 

DATA: subrc TYPE sy-subrc.

 

IF sy-subrc <> 0.

  CALL METHOD client->get_last_error

    IMPORTING

      code    = subrc

      MESSAGE = errortext.

  WRITE: / 'communication_error( send )',

         / 'code: ', subrc, 'message: '.

  EXIT.

ENDIF.

 

 

CALL METHOD client->receive

  EXCEPTIONS

    http_communication_failure = 1

    http_invalid_state         = 2

    http_processing_failed     = 3

    OTHERS                     = 4.

IF sy-subrc <> 0.

  CALL METHOD client->get_last_error

    IMPORTING

      code    = subrc

      MESSAGE = errortext.

  WRITE: / 'communication_error( receive )',

         / 'code: ', subrc, 'message: '.

  EXIT.

ENDIF.

 

DATA return_str TYPE string.

return_str = client->response->get_cdata( ).

* close

CALL METHOD client->close

  EXCEPTIONS

    http_invalid_state = 1

    OTHERS             = 2.

 

DATA html TYPE REF TO cl_gui_html_viewer.

DATA html_table TYPE TABLE OF char255.

 

CREATE OBJECT HTML TYPE CL_GUI_HTML_VIEWER.

 

CALL FUNCTION 'CONVERT_STRING_TO_TABLE'

  EXPORTING

    i_string         = return_str

    i_tabline_length = 255

  TABLES

    et_table         = html_table.

 

DATA: l_doc_url(255) TYPE c.

CALL METHOD html->load_data

  EXPORTING

    type         = 'text'

    subtype      = 'html'

  IMPORTING

    assigned_url = l_doc_url

  CHANGING

    data_table   = html_table.

 

CALL METHOD html->show_data

  EXPORTING

    url = l_doc_url.

 

 

 

 

 

 

 

 

 

 

 

 

2.            Server端构建

TCODE: SICF

Default Host/SAP/BC下建立节点ZADOBE_REQ

 

SE24: 建立类ZCL_HTTP_HANDLER,包含InterfaceIF_HTTP_EXTENSION

然后将该类放该服务的处理器列表中。

激活服务。

 

编写IF_HTTP_EXTENSION~HANDLE_REQUEST方法

*----------------------------------------------------------------------------------------------------------*

METHOD if_http_extension~handle_request.

 

  DATA: str  TYPE string,

        body TYPE string.

  DATA: t_table TYPE TABLE OF char255.

  DATA: cvto TYPE REF TO cl_abap_conv_out_ce.

  DATA: html_bin TYPE xstring.

  DATA: httptab  TYPE TIHTTPNVP,

        httpwa   TYPE IHTTPNVP.

 

  if_http_extension~lifetime_rc = if_http_extension=>co_lifetime_keep.

  if_http_extension~flow_rc     = if_http_extension=>co_flow_ok.

 

  str = server->request->get_cdata( ).

*  call method server->request->get_form_fields(

*                changing fields = httptab ).

*  IF str IS INITIAL.

*    CONCATENATE '<html><body>' '请填写数据然后SUBMIT' '</body></html>' INTO body.

*  ELSE.

*    loop at httptab into httpwa.

*      concatenate str httpwa-name httpwa-value into str.

*    endloop.

*    CONCATENATE '<html><body>' str '</body></html>' INTO body.

*  ENDIF.

 

  TRY.

      CALL METHOD cl_abap_conv_out_ce=>create

        EXPORTING

          encoding = '8400'

        RECEIVING

          conv     = cvto.

    CATCH cx_parameter_invalid_range .

    CATCH cx_sy_codepage_converter_init .

  ENDTRY.

 

  CALL METHOD server->response->set_header_field(

              name  = 'Content-Type'                        "#EC NOTEXT

              value = 'text/html' ).

 

  TRY.

      CALL METHOD cvto->write

        EXPORTING

          data = str.

    CATCH cx_sy_codepage_converter_init .

    CATCH cx_sy_conversion_codepage .

    CATCH cx_parameter_invalid_type .

    CATCH cx_parameter_invalid_range .

  ENDTRY.

* conversion into UTF-8 unicode code page

  html_bin = cvto->get_buffer( ).

 

* set body as hexadecimal string

  CALL METHOD server->response->set_data( data = html_bin ).

 

 

*  server->response->set_cdata( data = str ).

*  server->response->set_status( code = 200 reason = 'OK' ).

 

ENDMETHOD.

你可能感兴趣的:(【ABAP】Creat a client-server demo to process a http request using SAP Web AS)