Gyan Factory

Gyan Factory
SAP Technical Project Support

Thursday, December 31, 2015

RFC Connection Type -G and Using it in program with CL_HTTP_CLIENT to read an xml file from the Web

DATA :   lr_request TYPE REF TO if_http_request,
              lr_client TYPE REF TO if_http_client,
              lv_code TYPE i,
              lv_xdata TYPE xstring.


* Create a link to the source xml by the RFC destination
CALL METHOD cl_http_client=>create_by_destination
  EXPORTING
    destination              'ZW3SCHOOL'    " Logical destination (specified in function call)
  IMPORTING
    client                lr_client    " HTTP Client Abstraction
  EXCEPTIONS
    argument_not_found       1
    destination_not_found    2
    destination_no_authority 3
    plugin_not_active        4
    internal_error           5
    OTHERS                   6.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.


* Set the method type as Set
CALL METHOD lr_client->request->set_method
  EXPORTING
    method if_http_request=>co_request_method_get.



* Send the data
CALL METHOD lr_client->send
*  EXPORTING
*    timeout             = CO_TIMEOUT_DEFAULT     
  EXCEPTIONS
    http_communication_failure 1
    http_invalid_state         2
    http_processing_failed     3
    http_invalid_timeout       4
    OTHERS                     5.

IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.



* receive the data
CALL METHOD lr_client->receive
  EXCEPTIONS
    http_communication_failure 1
    http_invalid_state         2
    http_processing_failed     3
    OTHERS                     4.

IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.




CALL METHOD lr_client->response->get_status
  IMPORTING
    code lv_code.   " HTTP status code

IF lv_code 200.
* get the receving data
  CALL METHOD lr_client->response->get_data
*    EXPORTING
*      offset             = 0    " Offset into binary data
*      length             = -1    " Length of binary data
*      virus_scan_profile =     " Virus Scan Profile
    RECEIVING
      data               lv_xdata.    " Binary data



* Close the connection
  CALL METHOD lr_client->close
    EXCEPTIONS
      http_invalid_state 1
      OTHERS             2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.



* Display the data in xml format
  CALL FUNCTION 'DISPLAY_XML_STRING'
    EXPORTING
      xml_string      lv_xdata
      TITLE           'w3school xml file'
*     STARTING_X      = 5
*     STARTING_Y      = 5
    EXCEPTIONS
      no_xml_document 1
      OTHERS          2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDIF.
 


-------------------------------------------------------------------------------------------------------------------------------
Go to W3schools and open any xml file.


 Note down the file path name.
In Tx- SM59 create an rfc destination of type - G .

Provide the target host and the path of teh xml that we want to access. Do a connection test.

HTTP status is 200. Means connection is successful form abap server to the w3school.

Execute the above report which will read the xml file on the W3Schools and display it.


No comments:

Post a Comment