Skip to Content
Author's profile photo Guilherme Frisoni

How to Easy… Load an HTML Page into Internal Table

here is a simple code to load an HTML page into a internal table:

” Local Vars

DATA: lo_client  TYPE REF TO if_http_client,

       lc_url     TYPE string,

       lc_content TYPE string,

       lt_html    TYPE TABLE OF string.

” Set html page to load

lc_url = http://www.sap.com.

” Create the HTTP client

CALL METHOD cl_http_client=>create_by_url

   EXPORTING

     url    = lc_url

   IMPORTING

     client = lo_client

   EXCEPTIONS

     OTHERS = 1.

IF sysubrc IS NOT INITIAL.

   ” Error

   EXIT.

ENDIF.

” Set the Request type to GET

lo_client->request->set_header_field( name  = ‘~request_method’

                                       value = ‘GET’ ).

” Make the call

lo_client->send( ).

” Receive the Response Object

CALL METHOD lo_client->receive

   EXCEPTIONS

     http_communication_failure = 1

     http_invalid_state         = 2

     http_processing_failed     = 3

     OTHERS                     = 4.

IF sysubrc IS NOT INITIAL.

   ” Error

   EXIT.

ENDIF.

” Get the response content in Character format

lc_content = lo_client->response->get_cdata( ).

” Split content by lines in an internal table

SPLIT lc_content AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_html.

” From here you can parse you html code from table lt_html

It´s done.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jerry Baker
      Jerry Baker

      This was a great help, but I did find that the webpage I was trying to download required one small change. I had to replace 'cr_lf' with 'newline'.

      "Split content by lines in an internal table

      SPLIT lc_content AT cl_abap_char_utilities=>newline INTO TABLE lt_html.

      Author's profile photo koti padarthi
      koti padarthi

       

      This is great help, but I am getting error at  'CALL METHOD lo_client->receive' as HTTP_Communication_failure.

      Can you please help me out to fix this error.

      Author's profile photo Pankaj Sahita
      Pankaj Sahita

      I am also facing the exception HTTP_COMMUNICATION_FAILURE

      Could you please guide how to proceed.