Difference between revisions of "SAP ABAP HTTP"

From SapWiki
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Ejemplo HTTP_CLIENT==
 
==Ejemplo HTTP_CLIENT==
 +
===Ejemplo con SOAP===
 +
<nowiki> *URL              https://qa.sapwiki.cl:443/polosur/DLEApi.php
 +
*NAMESPACE        https://qa.sapwiki.cl/polosur/
 +
*HOST            qa.sapwiki.cl
 +
*SOAP ACTION      https://qa.sapwiki.cl/polosur/CargarDocumento
  
  URL              https://qa.sapwiki.cl:443/polosur/DLEApi.php
+
*----------------------------------------------------------------------*
  NAMESPACE        https://qa.sapwiki.cl/polosur/
+
*      FORM http_client
  HOST            qa.sapwiki.cl
+
*----------------------------------------------------------------------*
  SOAP ACTION      https://qa.sapwiki.cl/polosur/CargarDocumento
+
*      basado en programa RSWF_TEST_HTTP
 +
*----------------------------------------------------------------------*
 +
* --> p_url          Ej.: https://qa.sapwiki.cl:443/polosur/DLEApi.php
 +
* --> p_soap        String <soapenv:Envelope...</soapenv:Envelope>
 +
* --> p_soap_action  Parámetro SOAPAction:
 +
* --> p_host        Parámetro Host:
  
  *----------------------------------------------------------------------*
+
*  <-- p2        text
*      FORM http_client
+
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
+
 
*      basado en programa RSWF_TEST_HTTP
+
FORM http_client USING p_url
*----------------------------------------------------------------------*
+
                      p_soap
--> p_url          Ej.: https://qa.sapwiki.cl:443/polosur/DLEApi.php
+
                      p_soap_action
--> p_soap        String <soapenv:Envelope...</soapenv:Envelope>
+
                      p_host
--> p_soap_action  Parámetro SOAPAction:
+
              CHANGING p_subrc
*  --> p_host        Parámetro Host:
+
                      p_msg
+
                      p_response.
*  <--  p2        text
+
 
*----------------------------------------------------------------------*
+
  TYPE-POOLS: swfxc, icon.
+
  DATA: l_http_client        TYPE REF TO if_http_client.
FORM http_client USING p_url
+
  DATA: l_url                TYPE string.
                        p_soap
+
  DATA: l_code              TYPE sy-subrc.
                        p_soap_action
+
  DATA: l_code_string        TYPE string.
                        p_host
+
  DATA: l_message_string    TYPE string.
              CHANGING p_subrc
+
  DATA: lt_http_fields      TYPE tihttpnvp.
                        p_msg
+
  DATA: l_http_field_wa      TYPE ihttpnvp.
                        p_response.
+
  DATA: l_char_header(40)    TYPE c.
   
+
  DATA: l_body_string        TYPE string.
  TYPE-POOLS: swfxc, icon.
+
*-----------------------------------------------------------------------
  DATA: l_http_client        TYPE REF TO if_http_client.
+
*- Create the HTTP-Client
  DATA: l_url                TYPE string.
+
*-----------------------------------------------------------------------
  DATA: l_code              TYPE sy-subrc.
+
 
   DATA: l_code_string        TYPE string.
+
  CALL METHOD cl_http_client=>create_by_url
  DATA: l_message_string     TYPE string.
+
    EXPORTING
  DATA: lt_http_fields       TYPE tihttpnvp.
+
      url                = p_url
  DATA: l_http_field_wa      TYPE ihttpnvp.
+
*    PROXY_HOST        = l_proxy_host
   DATA: l_char_header(40)    TYPE c.
+
*    PROXY_SERVICE      = l_proxy_service
   DATA: l_body_string        TYPE string.
+
      ssl_id            = 'ANONYM'
*-----------------------------------------------------------------------
+
    IMPORTING
*- Create the HTTP-Client
+
      client            = l_http_client
*-----------------------------------------------------------------------
+
    EXCEPTIONS
   
+
      argument_not_found = 1
  CALL METHOD cl_http_client=>create_by_url
+
      plugin_not_active = 2
    EXPORTING
+
      internal_error    = 3
      url                = p_url
+
      OTHERS            = 4.
  *    PROXY_HOST        = l_proxy_host
+
  IF sy-subrc <> 0.
*     PROXY_SERVICE     = l_proxy_service
+
*   MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
      ssl_id            = 'ANONYM'
+
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
    IMPORTING
+
*            into l_message_string.
      client            = l_http_client
+
    IF l_message_string IS INITIAL.
    EXCEPTIONS
+
       l_message_string = 'Error creating HTTP-Client'.     "#EC NOTEXT
      argument_not_found = 1
+
    ENDIF.
      plugin_not_active  = 2
+
*   WRITE: / icon_red_light AS ICON.
      internal_error    = 3
+
*   PERFORM print_string USING l_message_string.
      OTHERS            = 4.
+
    p_msg = l_message_string.
  IF sy-subrc <> 0.
+
    p_subrc = 9.
  *    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
+
 
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
+
    EXIT.
  *            into l_message_string.
+
  ENDIF.
    IF l_message_string IS INITIAL.
+
 
      l_message_string = 'Error creating HTTP-Client'.      "#EC NOTEXT
+
*--------------------------------------------------------------------*
    ENDIF.
+
* HEADER
*    WRITE: / icon_red_light AS ICON.
+
*--------------------------------------------------------------------*
  *    PERFORM print_string USING l_message_string.
+
 
    p_msg = l_message_string.
+
* CALL METHOD l_http_client->request->set_header_field
    p_subrc = 9.
+
*    EXPORTING
+
*      name = '~request_method'
    EXIT.
+
*      value = 'POST'.
  ENDIF.
+
 
+
  DATA(l_bytes) = strlen( p_soap ).
*--------------------------------------------------------------------*
+
  DATA l_content_length TYPE string.
* HEADER
+
 
*--------------------------------------------------------------------*
+
  MOVE l_bytes TO l_content_length.
+
  CONDENSE l_content_length.
CALL METHOD l_http_client->request->set_header_field
+
 
*    EXPORTING
+
  CALL METHOD l_http_client->request->set_header_field
*     name  = '~request_method'
+
    EXPORTING
*     value = 'POST'.
+
      name = 'Accept-Encoding'
   
+
      value = 'gzip,deflate'.  "#EC NOTEXT
  DATA(l_bytes) = strlen( p_soap ).
+
 
  DATA l_content_length TYPE string.
+
  CALL METHOD l_http_client->request->set_header_field
+
    EXPORTING
  MOVE l_bytes TO l_content_length.
+
      name = 'Content-Type'
  CONDENSE l_content_length.
+
      value = 'text/xml;charset=UTF-8'. "#EC NOTEXT
+
 
   CALL METHOD l_http_client->request->set_header_field
+
  CALL METHOD l_http_client->request->set_header_field
     EXPORTING
+
    EXPORTING
       name  = 'Accept-Encoding'
+
      name = 'SOAPAction'
      value = 'gzip,deflate'.   "#EC NOTEXT
+
      value = p_soap_action. "#EC NOTEXT "'"https://qa.sapwiki.cl/polosur/CargarDocumento"'.
+
 
  CALL METHOD l_http_client->request->set_header_field
+
  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
+
    EXPORTING
      name = 'Content-Type'
+
      name  = 'Host'
      value = 'text/xml;charset=UTF-8'. "#EC NOTEXT
+
      value = p_host. "#EC NOTEXT "'qa.sapwiki.cl'.
+
 
   CALL METHOD l_http_client->request->set_header_field
+
  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
+
    EXPORTING
      name = 'SOAPAction'
+
      name  = 'Connection'
      value = p_soap_action. "#EC NOTEXT "'"https://qa.sapwiki.cl/polosur/CargarDocumento"'.
+
      value = 'Keep-Alive'.    "#EC NOTEXT
   
+
 
  CALL METHOD l_http_client->request->set_header_field
+
  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
+
    EXPORTING
      name  = 'Host'
+
      name  = 'User-Agent'
      value = p_host. "#EC NOTEXT "'qa.sapwiki.cl'.
+
      value = 'SAP NetWeaver Application Server (1.0;740)'. "#EC NOTEXT
+
 
  CALL METHOD l_http_client->request->set_header_field
+
  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
+
    EXPORTING
      name  = 'Connection'
+
      name  = 'Content-Length'
      value = 'Keep-Alive'.    "#EC NOTEXT
+
      value = l_content_length. "#EC NOTEXT "'30000'.
   
+
 
  CALL METHOD l_http_client->request->set_header_field
+
*--------------------------------------------------------------------*
    EXPORTING
+
* soapenvelope
      name  = 'User-Agent'
+
* string <soapenv:Envelope...</soapenv:Envelope>
      value = 'SAP NetWeaver Application Server (1.0;740)'. "#EC NOTEXT
+
*--------------------------------------------------------------------*
+
  CALL METHOD l_http_client->request->set_cdata
  CALL METHOD l_http_client->request->set_header_field
+
    EXPORTING
    EXPORTING
+
      data = p_soap.
      name  = 'Content-Length'
+
*    offset = 0
      value = l_content_length. "#EC NOTEXT "'30000'.
+
*   length = '19000'.
+
 
*--------------------------------------------------------------------*
+
** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V
* soapenvelope
+
** provide the compression settings to the HTTP client object
* string <soapenv:Envelope...</soapenv:Envelope>
+
** a) compress request
*--------------------------------------------------------------------*
+
*  IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true.
  CALL METHOD l_http_client->request->set_cdata
+
*   CALL METHOD l_http_client->set_compression
    EXPORTING
+
*     EXPORTING
      data = p_soap.
+
*       options                  = if_http_client=>co_compress_in_all_cases
*    offset = 0
+
*      EXCEPTIONS
*    length = '19000'.
+
*        compression_not_possible = 1.                       "#EC *
+
** ignore this exception to have the same behaviour as for clients created
** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V
+
** via sm59-destination: for this clients the method set_compression is
** provide the compression settings to the HTTP client object
+
** called within the method SEND without exception handling!
**  a) compress request
+
* ENDIF.
*  IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true.
+
*
*    CALL METHOD l_http_client->set_compression
+
*
*      EXPORTING
+
** b) compress response
*        options                  = if_http_client=>co_compress_in_all_cases
+
*  IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true.
*      EXCEPTIONS
+
*   l_http_client->propertytype_accept_compress = if_http_client=>co_enabled.
*        compression_not_possible = 1.                      "#EC *
+
* ELSE.
** ignore this exception to have the same behaviour as for clients created
+
**    m_client->propertytype_accept_compress = if_http_client=>co_disabled.
** via sm59-destination: for this clients the method set_compression is
+
* ENDIF.
** called within the method SEND without exception handling!
+
*
*  ENDIF.
+
** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8)
*
+
** the HTTP client is not allowed to follow an redirect automatically
*
+
** for methods other than HEAD and GET => prevent this for WS calls
** b) compress response
+
** NOTE: prevent the redirect also for PING functionalty although HEAD is
* IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true.
+
** used in this case in order to get an reliable PING-result
*    l_http_client->propertytype_accept_compress = if_http_client=>co_enabled.
+
* l_http_client->propertytype_redirect = if_http_client=>co_disabled.
*  ELSE.
+
*-----------------------------------------------------------------------
**    m_client->propertytype_accept_compress = if_http_client=>co_disabled.
+
*- send the http post
*  ENDIF.
+
*-----------------------------------------------------------------------
*
+
  CALL METHOD l_http_client->send
** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8)
+
    EXCEPTIONS
** the HTTP client is not allowed to follow an redirect automatically
+
      http_communication_failure = 1
** for methods other than HEAD and GET => prevent this for WS calls
+
      http_invalid_state        = 2.
** NOTE: prevent the redirect also for PING functionalty although HEAD is
+
  IF sy-subrc <> 0.
** used in this case in order to get an reliable PING-result
+
*    WRITE: / icon_red_light AS ICON.
*  l_http_client->propertytype_redirect = if_http_client=>co_disabled.
+
 
*-----------------------------------------------------------------------
+
    CALL METHOD l_http_client->get_last_error
*- send the http post
+
      IMPORTING
*-----------------------------------------------------------------------
+
        code    = l_code
  CALL METHOD l_http_client->send
+
        message = l_message_string.
    EXCEPTIONS
+
 
      http_communication_failure = 1
+
    CALL METHOD l_http_client->close.
      http_invalid_state        = 2.
+
 
  IF sy-subrc <> 0.
+
    l_code_string = l_code.
*    WRITE: / icon_red_light AS ICON.
+
    CONCATENATE 'HTTP-Send: RC=' l_code_string              "#EC NOTEXT
+
          INTO l_code_string .
    CALL METHOD l_http_client->get_last_error
+
    CONCATENATE l_code_string l_message_string
      IMPORTING
+
          INTO l_message_string SEPARATED BY space.
        code    = l_code
+
 
        message = l_message_string.
+
*   PERFORM print_string USING l_message_string.
+
    p_msg = l_message_string.
    CALL METHOD l_http_client->close.
+
    p_subrc = 8.
+
    EXIT.
    l_code_string = l_code.
+
  ENDIF.
    CONCATENATE 'HTTP-Send: RC=' l_code_string              "#EC NOTEXT
+
 
            INTO l_code_string .
+
*-----------------------------------------------------------------------
    CONCATENATE l_code_string l_message_string
+
*- receive the result of http post
            INTO l_message_string SEPARATED BY space.
+
*-----------------------------------------------------------------------
+
  CALL METHOD l_http_client->receive
*    PERFORM print_string USING l_message_string.
+
    EXCEPTIONS
    p_msg = l_message_string.
+
      http_communication_failure = 1
    p_subrc = 8.
+
      http_invalid_state        = 2.
    EXIT.
+
  IF sy-subrc <> 0.
  ENDIF.
+
*   WRITE: / icon_red_light AS ICON.
+
 
*-----------------------------------------------------------------------
+
    CALL METHOD l_http_client->get_last_error
*- receive the result of http post
+
      IMPORTING
*-----------------------------------------------------------------------
+
        code    = l_code
   CALL METHOD l_http_client->receive
+
        message = l_message_string.
    EXCEPTIONS
+
 
      http_communication_failure = 1
+
    CALL METHOD l_http_client->close.
      http_invalid_state        = 2.
+
 
  IF sy-subrc <> 0.
+
    l_code_string = l_code.
*   WRITE: / icon_red_light AS ICON.
+
    CONCATENATE 'HTTP-Receive: RC=' l_code_string          "#EC NOTEXT
   
+
          INTO l_code_string .
    CALL METHOD l_http_client->get_last_error
+
    CONCATENATE l_code_string l_message_string
      IMPORTING
+
          INTO l_message_string SEPARATED BY space.
        code   = l_code
+
 
        message = l_message_string.
+
*    PERFORM print_string USING l_message_string.
   
+
    p_msg = l_message_string.
    CALL METHOD l_http_client->close.
+
    p_subrc = 7.
+
    EXIT.
    l_code_string = l_code.
+
  ENDIF.
    CONCATENATE 'HTTP-Receive: RC=' l_code_string          "#EC NOTEXT
+
 
            INTO l_code_string .
+
*-----------------------------------------------------------------------
    CONCATENATE l_code_string l_message_string
+
*- print the results
            INTO l_message_string SEPARATED BY space.
+
*-----------------------------------------------------------------------
+
  CALL METHOD l_http_client->response->get_status
*   PERFORM print_string USING l_message_string.
+
    IMPORTING
    p_msg = l_message_string.
+
      code = l_code.
    p_subrc = 7.
+
  IF l_code < 300.
    EXIT.
+
*  HTTP-codes: 100 - 199 = informations
  ENDIF.
+
*  HTTP-codes: 200 - 299 = client-request successful (200 = OK)
+
*    WRITE: / icon_green_light AS ICON.
*-----------------------------------------------------------------------
+
    p_subrc = 0.
*- print the results
+
  ELSE.
*-----------------------------------------------------------------------
+
*  HTTP-codes: 300 - 399 = redirected; further actions required
  CALL METHOD l_http_client->response->get_status
+
*  HTTP-codes: 400 - 499 = client-request incomplete
    IMPORTING
+
*  HTTP-codes: 500 - 599 = server errors
      code = l_code.
+
*   WRITE: / icon_red_light AS ICON.
  IF l_code < 300.
+
*   IF l_code BETWEEN 300 AND 399.
*   HTTP-codes: 100 - 199 = informations
+
*      p_msg = 'Redirected; further actions required'.      "#EC NOTEXT
*  HTTP-codes: 200 - 299 = client-request successful (200 = OK)
+
*    ELSEIF  l_code BETWEEN 400 AND 499.
*    WRITE: / icon_green_light AS ICON.
+
*      p_msg = 'Client-request incomplete'.                  "#EC NOTEXT
    p_subrc = 0.
+
*    ELSE.
  ELSE.
+
*      p_msg = 'Server errors'.                              "#EC NOTEXT
*  HTTP-codes: 300 - 399 = redirected; further actions required
+
*   ENDIF.
*  HTTP-codes: 400 - 499 = client-request incomplete
+
*
*  HTTP-codes: 500 - 599 = server errors
+
*    p_msg = |Web Service: Error { l_code } { p_msg }|.      "#EC NOTEXT
*    WRITE: / icon_red_light AS ICON.
+
 
*    IF l_code BETWEEN 300 AND 399.
+
    p_subrc = 6.
*      p_msg = 'Redirected; further actions required'.       "#EC NOTEXT
+
  ENDIF.
*   ELSEIF  l_code BETWEEN 400 AND 499.
+
*-- get the http header fields
*      p_msg = 'Client-request incomplete'.                  "#EC NOTEXT
+
* CALL METHOD l_http_client->response->get_header_fields
*    ELSE.
+
*   CHANGING
*      p_msg = 'Server errors'.                              "#EC NOTEXT
+
*      fields = lt_http_fields.
*    ENDIF.
+
* LOOP AT lt_http_fields INTO l_http_field_wa.
*
+
*    l_char_header = l_http_field_wa-name.
  *    p_msg = |Web Service: Error { l_code } { p_msg }|.      "#EC NOTEXT
+
*    WRITE: / l_char_header.
+
*    l_char_header = l_http_field_wa-value.
    p_subrc = 6.
+
*    WRITE: l_char_header.
  ENDIF.
+
*  ENDLOOP.
*-- get the http header fields
+
*  WRITE: / sy-uline.
* CALL METHOD l_http_client->response->get_header_fields
+
 
*   CHANGING
+
*- get the body
*      fields = lt_http_fields.
+
  IF p_subrc = 0.
*  LOOP AT lt_http_fields INTO l_http_field_wa.
+
    l_body_string = l_http_client->response->get_cdata(  ).
*    l_char_header = l_http_field_wa-name.
+
*  PERFORM print_string USING l_body_string.
*    WRITE: / l_char_header.
+
    p_response = l_body_string.
*    l_char_header = l_http_field_wa-value.
+
  ELSE.
*    WRITE: l_char_header.
+
*-- get the http header fields
*  ENDLOOP.
+
    CALL METHOD l_http_client->response->get_header_fields
*  WRITE: / sy-uline.
+
      CHANGING
+
        fields = lt_http_fields.
*- get the body
+
    LOOP AT lt_http_fields INTO l_http_field_wa.
  IF p_subrc = 0.
+
      p_msg = |Webservice: Error { l_http_field_wa-value }|.
    l_body_string = l_http_client->response->get_cdata(  ).
+
      EXIT.
*  PERFORM print_string USING l_body_string.
+
    ENDLOOP.
    p_response = l_body_string.
+
  ENDIF.
  ELSE.
+
 
*-- get the http header fields
+
ENDFORM.</nowiki>
    CALL METHOD l_http_client->response->get_header_fields
+
===Ejemplo con POST===
      CHANGING
+
<nowiki>
        fields = lt_http_fields.
+
  METHOD http_client.
    LOOP AT lt_http_fields INTO l_http_field_wa.
+
    TYPE-POOLS: swfxc, icon.
      p_msg = |Webservice: Error { l_http_field_wa-value }|.
+
    DATA: l_http_client        TYPE REF TO if_http_client.
      EXIT.
+
    DATA: l_url                TYPE string.
    ENDLOOP.
+
    DATA: l_code              TYPE sy-subrc.
  ENDIF.
+
    DATA: l_code_string        TYPE string.
+
    DATA: l_message_string    TYPE string.
ENDFORM.
+
    DATA: lt_http_fields      TYPE tihttpnvp.
 +
    DATA: l_http_field_wa      TYPE ihttpnvp.
 +
    DATA: l_char_header(40)    TYPE c.
 +
    DATA: l_body_string        TYPE string.
 +
*-----------------------------------------------------------------------
 +
*- Create the HTTP-Client
 +
*-----------------------------------------------------------------------
 +
 
 +
    CALL METHOD cl_http_client=>create_by_url
 +
       EXPORTING
 +
        url                = i_url
 +
*       PROXY_HOST        = l_proxy_host
 +
*       PROXY_SERVICE     = l_proxy_service
 +
        ssl_id            = 'ANONYM'
 +
      IMPORTING
 +
        client            = l_http_client
 +
      EXCEPTIONS
 +
        argument_not_found = 1
 +
        plugin_not_active = 2
 +
        internal_error    = 3
 +
        OTHERS            = 4.
 +
    IF sy-subrc <> 0.
 +
*   MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
 +
*           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
 +
*           into l_message_string.
 +
      IF l_message_string IS INITIAL.
 +
        l_message_string = 'Error creating HTTP-Client'.    "#EC NOTEXT
 +
      ENDIF.
 +
*    WRITE: / icon_red_light AS ICON.
 +
*    PERFORM print_string USING l_message_string.
 +
      e_msg = l_message_string.
 +
      e_subrc = 9.
 +
 
 +
      EXIT.
 +
    ENDIF.
 +
 
 +
*--------------------------------------------------------------------*
 +
* HEADER
 +
*--------------------------------------------------------------------*
 +
 
 +
*    l_http_client->request->set_method( 'POST' ).
 +
    l_http_client->request->set_method( i_method ).
 +
 
 +
*    l_http_client->request->set_content_type( 'application/json' ).
 +
 
 +
    IF i_content_type IS NOT INITIAL.
 +
      l_http_client->request->set_content_type( i_content_type ).
 +
    ENDIF.
 +
 
 +
    IF i_x_api_key IS SUPPLIED.
 +
      CALL METHOD l_http_client->request->set_header_field
 +
        EXPORTING
 +
          name  = i_x_api_key
 +
          value = i_api_key. "#EC NOTEXT
 +
    ELSE.
 +
      CALL METHOD l_http_client->request->set_header_field
 +
        EXPORTING
 +
          name  = 'X-API-KEY'
 +
          value = i_api_key. "#EC NOTEXT
 +
    ENDIF.
 +
 
 +
    IF i_content_type IS NOT INITIAL.
 +
      CALL METHOD l_http_client->request->set_header_field
 +
        EXPORTING
 +
          name  = 'Content-Type'
 +
          value = 'application/json'. "#EC NOTEXT
 +
    ENDIF.
 +
 
 +
*--------------------------------------------------------------------*
 +
* soapenvelope
 +
* string <soapenv:Envelope...</soapenv:Envelope>
 +
*--------------------------------------------------------------------*
 +
    IF i_body IS SUPPLIED.
 +
      CALL METHOD l_http_client->request->set_cdata
 +
        EXPORTING
 +
          data = i_body.
 +
    ENDIF.
 +
*    offset = 0
 +
*    length = '19000'.
 +
 
 +
** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V
 +
** provide the compression settings to the HTTP client object
 +
**  a) compress request
 +
*  IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true.
 +
*    CALL METHOD l_http_client->set_compression
 +
*      EXPORTING
 +
*        options                  = if_http_client=>co_compress_in_all_cases
 +
*      EXCEPTIONS
 +
*        compression_not_possible = 1.                      "#EC *
 +
** ignore this exception to have the same behaviour as for clients created
 +
** via sm59-destination: for this clients the method set_compression is
 +
** called within the method SEND without exception handling!
 +
*  ENDIF.
 +
*
 +
*
 +
** b) compress response
 +
*  IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true.
 +
*    l_http_client->propertytype_accept_compress = if_http_client=>co_enabled.
 +
*  ELSE.
 +
**    m_client->propertytype_accept_compress = if_http_client=>co_disabled.
 +
*  ENDIF.
 +
*
 +
** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8)
 +
** the HTTP client is not allowed to follow an redirect automatically
 +
** for methods other than HEAD and GET => prevent this for WS calls
 +
** NOTE: prevent the redirect also for PING functionalty although HEAD is
 +
** used in this case in order to get an reliable PING-result
 +
*  l_http_client->propertytype_redirect = if_http_client=>co_disabled.
 +
*-----------------------------------------------------------------------
 +
*- send the http post
 +
*-----------------------------------------------------------------------
 +
    CALL METHOD l_http_client->send
 +
      EXCEPTIONS
 +
        http_communication_failure = 1
 +
        http_invalid_state        = 2.
 +
    IF sy-subrc <> 0.
 +
*    WRITE: / icon_red_light AS ICON.
 +
 
 +
      CALL METHOD l_http_client->get_last_error
 +
        IMPORTING
 +
          code    = l_code
 +
          message = l_message_string.
 +
 
 +
      CALL METHOD l_http_client->close.
 +
 
 +
      l_code_string = l_code.
 +
      CONCATENATE 'HTTP-Send: RC=' l_code_string            "#EC NOTEXT
 +
            INTO l_code_string .
 +
      CONCATENATE l_code_string l_message_string
 +
            INTO l_message_string SEPARATED BY space.
 +
 
 +
*    PERFORM print_string USING l_message_string.
 +
      e_msg = l_message_string.
 +
      e_subrc = 8.
 +
      EXIT.
 +
    ENDIF.
 +
 
 +
*-----------------------------------------------------------------------
 +
*- receive the result of http post
 +
*-----------------------------------------------------------------------
 +
    CALL METHOD l_http_client->receive
 +
      EXCEPTIONS
 +
        http_communication_failure = 1
 +
        http_invalid_state        = 2.
 +
    IF sy-subrc <> 0.
 +
*    WRITE: / icon_red_light AS ICON.
 +
 
 +
      CALL METHOD l_http_client->get_last_error
 +
        IMPORTING
 +
          code    = l_code
 +
          message = l_message_string.
 +
 
 +
      CALL METHOD l_http_client->close.
 +
 
 +
      l_code_string = l_code.
 +
      CONCATENATE 'HTTP-Receive: RC=' l_code_string        "#EC NOTEXT
 +
            INTO l_code_string .
 +
      CONCATENATE l_code_string l_message_string
 +
            INTO l_message_string SEPARATED BY space.
 +
 
 +
*    PERFORM print_string USING l_message_string.
 +
      e_msg = l_message_string.
 +
      e_subrc = 7.
 +
      EXIT.
 +
    ENDIF.
 +
 
 +
*-----------------------------------------------------------------------
 +
*- print the results
 +
*-----------------------------------------------------------------------
 +
    CALL METHOD l_http_client->response->get_status
 +
      IMPORTING
 +
        code = l_code.
 +
    IF l_code < 300 OR l_code = 400. "bad request
 +
*  HTTP-codes: 100 - 199 = informations
 +
*  HTTP-codes: 200 - 299 = client-request successful (200 = OK)
 +
*    WRITE: / icon_green_light AS ICON.
 +
      e_subrc = 0.
 +
    ELSE.
 +
*  HTTP-codes: 300 - 399 = redirected; further actions required
 +
*  HTTP-codes: 400 - 499 = client-request incomplete
 +
*  HTTP-codes: 500 - 599 = server errors
 +
*    WRITE: / icon_red_light AS ICON.
 +
*    IF l_code BETWEEN 300 AND 399.
 +
*      p_msg = 'Redirected; further actions required'.      "#EC NOTEXT
 +
*    ELSEIF  l_code BETWEEN 400 AND 499.
 +
*      p_msg = 'Client-request incomplete'.                  "#EC NOTEXT
 +
*    ELSE.
 +
*      p_msg = 'Server errors'.                              "#EC NOTEXT
 +
*    ENDIF.
 +
*
 +
*    p_msg = |Cloudsigner: Error { l_code } { p_msg }|.      "#EC NOTEXT
 +
 
 +
      e_subrc = 6.
 +
    ENDIF.
 +
*-- get the http header fields
 +
*  CALL METHOD l_http_client->response->get_header_fields
 +
*    CHANGING
 +
*      fields = lt_http_fields.
 +
*  LOOP AT lt_http_fields INTO l_http_field_wa.
 +
*    l_char_header = l_http_field_wa-name.
 +
*    WRITE: / l_char_header.
 +
*    l_char_header = l_http_field_wa-value.
 +
*    WRITE: l_char_header.
 +
*  ENDLOOP.
 +
*  WRITE: / sy-uline.
 +
 
 +
*- get the body
 +
    IF e_subrc = 0.
 +
      l_body_string = l_http_client->response->get_cdata(  ).
 +
*  PERFORM print_string USING l_body_string.
 +
      e_response = l_body_string.
 +
    ELSE.
 +
*-- get the http header fields
 +
      CALL METHOD l_http_client->response->get_header_fields
 +
        CHANGING
 +
          fields = lt_http_fields.
 +
      LOOP AT lt_http_fields INTO l_http_field_wa.
 +
        e_msg = |Acepta: Error { l_http_field_wa-value }|.
 +
        EXIT.
 +
      ENDLOOP.
 +
    ENDIF.
 +
  ENDMETHOD.
 +
</nowiki>
 +
===Ejemplo con GET y obtener PDF (binario)===
 +
<nowiki>
 +
  METHOD http_client.
 +
    TYPE-POOLS: swfxc, icon.
 +
    DATA: l_http_client        TYPE REF TO if_http_client.
 +
    DATA: l_url                TYPE string.
 +
    DATA: l_code              TYPE sy-subrc.
 +
    DATA: l_code_string        TYPE string.
 +
    DATA: l_message_string    TYPE string.
 +
    DATA: lt_http_fields      TYPE tihttpnvp.
 +
    DATA: l_http_field_wa      TYPE ihttpnvp.
 +
    DATA: l_char_header(40)    TYPE c.
 +
    DATA: l_body_string        TYPE string.
 +
    DATA: l_body_xstring        TYPE xstring.
 +
*-----------------------------------------------------------------------
 +
*- Create the HTTP-Client
 +
*-----------------------------------------------------------------------
 +
    DATA ssl_id TYPE ssfapplssl.
 +
    ssl_id = 'ANONYM'.
 +
    CALL METHOD cl_http_client=>create_by_url
 +
      EXPORTING
 +
        url                = i_url
 +
      ssl_id            = ssl_id  "'ANONYM'
 +
      IMPORTING
 +
        client            = l_http_client
 +
      EXCEPTIONS
 +
        argument_not_found = 1
 +
        plugin_not_active  = 2
 +
        internal_error    = 3
 +
        OTHERS            = 4.
 +
    IF sy-subrc <> 0.
 +
      IF l_message_string IS INITIAL.
 +
        l_message_string = 'Error creating HTTP-Client'.    "#EC NOTEXT
 +
      ENDIF.
 +
 
 +
      e_msg = l_message_string.
 +
      e_subrc = 9.
 +
 
 +
      EXIT.
 +
    ENDIF.
 +
 
 +
*--------------------------------------------------------------------*
 +
* HEADER
 +
*--------------------------------------------------------------------*
 +
    IF i_method IS NOT INITIAL.
 +
      l_http_client->request->set_method( i_method ).
 +
    ENDIF.
 +
 
 +
    IF i_content_type IS NOT INITIAL.
 +
      l_http_client->request->set_content_type( i_content_type ).
 +
    ENDIF.
 +
 
 +
    IF i_api_key_key IS NOT INITIAL.
 +
      CALL METHOD l_http_client->request->set_header_field
 +
        EXPORTING
 +
          name  = i_api_key_key
 +
          value = i_api_key_value. "#EC NOTEXT
 +
    ENDIF.
 +
 
 +
*--------------------------------------------------------------------*
 +
* soapenvelope
 +
* string <soapenv:Envelope...</soapenv:Envelope>
 +
*--------------------------------------------------------------------*
 +
    IF i_body IS SUPPLIED.
 +
      CALL METHOD l_http_client->request->set_cdata
 +
        EXPORTING
 +
          data = i_body.
 +
    ENDIF.
 +
 
 +
*-----------------------------------------------------------------------
 +
*- send the http post
 +
*-----------------------------------------------------------------------
 +
    CALL METHOD l_http_client->send
 +
      EXCEPTIONS
 +
        http_communication_failure = 1
 +
        http_invalid_state        = 2.
 +
    IF sy-subrc <> 0.
 +
      CALL METHOD l_http_client->get_last_error
 +
        IMPORTING
 +
          code    = l_code
 +
          message = l_message_string.
 +
 
 +
      CALL METHOD l_http_client->close.
 +
 
 +
      l_code_string = l_code.
 +
      CONCATENATE 'HTTP-Send: RC=' l_code_string            "#EC NOTEXT
 +
            INTO l_code_string .
 +
      CONCATENATE l_code_string l_message_string
 +
            INTO l_message_string SEPARATED BY space.
 +
 
 +
      e_msg = l_message_string.
 +
      e_subrc = 8.
 +
      EXIT.
 +
    ENDIF.
 +
 
 +
*-----------------------------------------------------------------------
 +
*- receive the result of http post
 +
*-----------------------------------------------------------------------
 +
    CALL METHOD l_http_client->receive
 +
      EXCEPTIONS
 +
        http_communication_failure = 1
 +
        http_invalid_state        = 2.
 +
    IF sy-subrc <> 0.
 +
 
 +
      CALL METHOD l_http_client->get_last_error
 +
        IMPORTING
 +
          code    = l_code
 +
          message = l_message_string.
 +
 
 +
      CALL METHOD l_http_client->close.
 +
 
 +
      l_code_string = l_code.
 +
      CONCATENATE 'HTTP-Receive: RC=' l_code_string        "#EC NOTEXT
 +
            INTO l_code_string .
 +
      CONCATENATE l_code_string l_message_string
 +
            INTO l_message_string SEPARATED BY space.
 +
 
 +
      e_msg = l_message_string.
 +
      e_subrc = 7.
 +
      EXIT.
 +
    ENDIF.
 +
 
 +
*-----------------------------------------------------------------------
 +
*- print the results
 +
*-----------------------------------------------------------------------
 +
    CALL METHOD l_http_client->response->get_status
 +
      IMPORTING
 +
        code = l_code.
 +
    IF l_code < 300 OR l_code = 400. "bad request
 +
*  HTTP-codes: 100 - 199 = informations
 +
*  HTTP-codes: 200 - 299 = client-request successful (200 = OK)
 +
*    WRITE: / icon_green_light AS ICON.
 +
      e_subrc = 0.
 +
    ELSE.
 +
*  HTTP-codes: 300 - 399 = redirected; further actions required
 +
*  HTTP-codes: 400 - 499 = client-request incomplete
 +
*  HTTP-codes: 500 - 599 = server errors
 +
      e_subrc = 6.
 +
    ENDIF.
 +
   
 +
*- get the body
 +
    IF e_subrc = 0.
 +
      l_body_xstring = l_http_client->response->get_data(  ).
 +
      e_xresponse = l_body_xstring.
 +
    ELSE.
 +
*-- get the http header fields
 +
      CALL METHOD l_http_client->response->get_header_fields
 +
        CHANGING
 +
          fields = lt_http_fields.
 +
      LOOP AT lt_http_fields INTO l_http_field_wa.
 +
        e_msg = |Error { l_http_field_wa-value }|.
 +
        EXIT.
 +
      ENDLOOP.
 +
    ENDIF.
 +
  ENDMETHOD.
 +
 
 +
  CALL METHOD zcl_obtener_documento=>http_client
 +
    EXPORTING
 +
      i_url      = l_url "https://www.sapwiki.cl/documentos/ObtenerDocumento/Id=12345678'.
 +
*    i_body      =
 +
      i_api_key_key  = l_api_key_key
 +
      i_api_key_value = l_api_key_value
 +
      i_method    = 'GET'
 +
    i_content_type = l_content_type  "space
 +
    IMPORTING
 +
      e_msg      = e_msg
 +
      e_xresponse = e_xdocumento
 +
      e_subrc    = e_subrc.
 +
</nowiki>
 +
==Llamada a proxy client==
 +
 
 +
<nowiki>* proxy client
 +
  DATA: lo_proxy TYPE REF TO zhcm_co_si_os_carga_documento,
 +
        ls_output TYPE zhcm_mt_carga_documento,
 +
        ls_input  TYPE zhcm_mt_respuesta_documento,
 +
        lo_system_ex TYPE REF TO cx_ai_system_fault,
 +
        lo_app_ex TYPE REF TO cx_ai_application_fault.
 +
 
 +
*do something
 +
*.....
 +
*
 +
 
 +
  TRY.
 +
* create proxy
 +
      CREATE OBJECT lo_proxy.
 +
 
 +
      CALL METHOD lo_proxy->si_os_carga_documento
 +
        EXPORTING
 +
          output = ls_output
 +
        IMPORTING
 +
          input  = ls_input.
 +
    CATCH cx_ai_system_fault  INTO lo_system_ex.
 +
*      WRITE lo_system_ex->errortext TO msg.
 +
*      msg =  lo_system_ex->if_message~get_text( ).
 +
    CATCH cx_ai_application_fault INTO lo_app_ex.
 +
*      WRITE  lo_app_ex->textid TO msg.
 +
*      msg = lo_app_ex->if_message~get_text( ).
 +
  ENDTRY.
 +
 
 +
 
 +
  IF lo_system_ex IS NOT INITIAL.
 +
    e_message =  lo_system_ex->if_message~get_text( ).
 +
    e_subrc = 1.
 +
    RETURN.
 +
  ENDIF.
 +
 
 +
  IF lo_app_ex IS NOT INITIAL.
 +
    e_message =  lo_app_ex->if_message~get_text( ).
 +
    e_subrc = 2.
 +
    RETURN.
 +
  ENDIF.</nowiki>
 +
==clase cl_abap_browser=>show_url==
 +
<nowiki>
 +
DATA: lv_title TYPE cl_abap_browser=>title,
 +
      lv_url  TYPE string.
 +
PARAMETERS p_url TYPE string OBLIGATORY LOWER CASE.
 +
 
 +
*--------------------------------------------------------------------*
 +
INITIALIZATION.
 +
*--------------------------------------------------------------------*
 +
  p_url = 'https://homer.sii.cl/'.
 +
  lv_title = 'example'.
 +
 
 +
START-OF-SELECTION.
 +
 
 +
  CALL METHOD cl_abap_browser=>show_url
 +
    EXPORTING
 +
      url          = p_url
 +
      title        = lv_title
 +
      size        = cl_abap_browser=>xlarge
 +
*    modal        = iv_sapgui_show_in_dialog
 +
      buttons      = cl_abap_browser=>navigate_off
 +
      format      = cl_abap_browser=>landscape
 +
      position    = cl_abap_browser=>topleft
 +
      context_menu = abap_true.</nowiki>
 +
==Notas PROXY==
 +
* SRT_UTIL    tran. log de llamada proxy
 +
* Tabla SPROXXSL : relación entre proxy & XSLT hay que concatenar  /1SAI/TXS & NAME e ir a la XSLT_TOOL
 +
* Si en llamada a proxy da error de deserialization, poner break en CL_SXMLP_DATA_ST==============CM004 línea 26
 +
* RSSIDL_DESERIALIZE_DEMO  verificar WSDL, ver nota Ver nota 1801866 - Proxy generation terminated: Incorrect value: Unknown Type Reference
 +
* Test conexión HTML, reporte RSWF_TEST_HTTP
 +
* LOG ICM : transacción SMICM, en menú: Pasar a -->Fichero trace-->Visualizar todo
 +
* Configuración Proxy : transacción SICF
 +
* Trn. STRUST Instalar certificados
 +
* Nota 2469949 - "ICF Error when creating HTTP client object by Config for URL" in Web Services ABAP
 +
* Nota 2334940 - B2A: Troubleshooting HTTPS for communication with health insurance funds
 +
* Nota 1714792 - Proxy generation terminated: Message must have exactly one part
 +
* Nota 1817447 - Error "GET_BUSINESS_SYSTEM_ERROR" for Consumer Proxy calls
 +
* Nota 2033184 - How to create Logical Port manually for a Service Consumer in SOAMANAGER [Video]
 +
* Nota 1791729 - WDA FileUpload error: "Error when uploading; the file name is invalid or the file is too large"
 +
* Nota 2322379 - How to create payload trace using SRT_UTIL in AS ABAP
 +
* Nota 2368112 - Outgoing HTTPS connection does not work in AS ABAP
 +
* Nota 2388992 - WSDL parsing error: Exception in library handler
 +
* Nota 2508091 - Web Services ABAP: Error "Element is missing" CX_ST_GROUP_MISSING_CASE
 +
* Nota 2553979 - SOAP Web Services ABAP - Guided Answers
 +
 
 +
==Varios==
 +
clase CL_GUI_HTML_VIEWER método SHOW_URL para ver URL

Latest revision as of 20:52, 26 August 2024

Ejemplo HTTP_CLIENT

Ejemplo con SOAP

 *URL              https://qa.sapwiki.cl:443/polosur/DLEApi.php
 *NAMESPACE        https://qa.sapwiki.cl/polosur/
 *HOST             qa.sapwiki.cl
 *SOAP ACTION      https://qa.sapwiki.cl/polosur/CargarDocumento

*----------------------------------------------------------------------*
*       FORM http_client
*----------------------------------------------------------------------*
*       basado en programa RSWF_TEST_HTTP
*----------------------------------------------------------------------*
*  --> p_url          Ej.: https://qa.sapwiki.cl:443/polosur/DLEApi.php
*  --> p_soap         String <soapenv:Envelope...</soapenv:Envelope>
*  --> p_soap_action  Parámetro SOAPAction:
*  --> p_host         Parámetro Host:

*  <--  p2        text
*----------------------------------------------------------------------*

FORM http_client USING p_url
                       p_soap
                       p_soap_action
                       p_host
              CHANGING p_subrc
                       p_msg
                       p_response.

  TYPE-POOLS: swfxc, icon.
  DATA: l_http_client        TYPE REF TO if_http_client.
  DATA: l_url                TYPE string.
  DATA: l_code               TYPE sy-subrc.
  DATA: l_code_string        TYPE string.
  DATA: l_message_string     TYPE string.
  DATA: lt_http_fields       TYPE tihttpnvp.
  DATA: l_http_field_wa      TYPE ihttpnvp.
  DATA: l_char_header(40)    TYPE c.
  DATA: l_body_string        TYPE string.
*-----------------------------------------------------------------------
*- Create the HTTP-Client
*-----------------------------------------------------------------------

  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = p_url
*     PROXY_HOST         = l_proxy_host
*     PROXY_SERVICE      = l_proxy_service
      ssl_id             = 'ANONYM'
    IMPORTING
      client             = l_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
  IF sy-subrc <> 0.
*    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
*            into l_message_string.
    IF l_message_string IS INITIAL.
      l_message_string = 'Error creating HTTP-Client'.      "#EC NOTEXT
    ENDIF.
*    WRITE: / icon_red_light AS ICON.
*    PERFORM print_string USING l_message_string.
    p_msg = l_message_string.
    p_subrc = 9.

    EXIT.
  ENDIF.

*--------------------------------------------------------------------*
* HEADER
*--------------------------------------------------------------------*

*  CALL METHOD l_http_client->request->set_header_field
*    EXPORTING
*      name  = '~request_method'
*      value = 'POST'.

  DATA(l_bytes) = strlen( p_soap ).
  DATA l_content_length TYPE string.

  MOVE l_bytes TO l_content_length.
  CONDENSE l_content_length.

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Accept-Encoding'
      value = 'gzip,deflate'.   "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'text/xml;charset=UTF-8'. "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'SOAPAction'
      value = p_soap_action. "#EC NOTEXT "'"https://qa.sapwiki.cl/polosur/CargarDocumento"'.

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Host'
      value = p_host. "#EC NOTEXT "'qa.sapwiki.cl'.

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Connection'
      value = 'Keep-Alive'.    "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'User-Agent'
      value = 'SAP NetWeaver Application Server (1.0;740)'. "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Length'
      value = l_content_length. "#EC NOTEXT "'30000'.

*--------------------------------------------------------------------*
* soapenvelope
* string <soapenv:Envelope...</soapenv:Envelope>
*--------------------------------------------------------------------*
  CALL METHOD l_http_client->request->set_cdata
    EXPORTING
      data = p_soap.
*    offset = 0
*    length = '19000'.

** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V
** provide the compression settings to the HTTP client object
**  a) compress request
*  IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true.
*    CALL METHOD l_http_client->set_compression
*      EXPORTING
*        options                  = if_http_client=>co_compress_in_all_cases
*      EXCEPTIONS
*        compression_not_possible = 1.                       "#EC *
** ignore this exception to have the same behaviour as for clients created
** via sm59-destination: for this clients the method set_compression is
** called within the method SEND without exception handling!
*  ENDIF.
*
*
** b) compress response
*  IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true.
*    l_http_client->propertytype_accept_compress = if_http_client=>co_enabled.
*  ELSE.
**    m_client->propertytype_accept_compress = if_http_client=>co_disabled.
*  ENDIF.
*
** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8)
** the HTTP client is not allowed to follow an redirect automatically
** for methods other than HEAD and GET => prevent this for WS calls
** NOTE: prevent the redirect also for PING functionalty although HEAD is
** used in this case in order to get an reliable PING-result
*  l_http_client->propertytype_redirect = if_http_client=>co_disabled.
*-----------------------------------------------------------------------
*- send the http post
*-----------------------------------------------------------------------
  CALL METHOD l_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  IF sy-subrc <> 0.
*    WRITE: / icon_red_light AS ICON.

    CALL METHOD l_http_client->get_last_error
      IMPORTING
        code    = l_code
        message = l_message_string.

    CALL METHOD l_http_client->close.

    l_code_string = l_code.
    CONCATENATE 'HTTP-Send: RC=' l_code_string              "#EC NOTEXT
           INTO l_code_string .
    CONCATENATE l_code_string l_message_string
           INTO l_message_string SEPARATED BY space.

*    PERFORM print_string USING l_message_string.
    p_msg = l_message_string.
    p_subrc = 8.
    EXIT.
  ENDIF.

*-----------------------------------------------------------------------
*- receive the result of http post
*-----------------------------------------------------------------------
  CALL METHOD l_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  IF sy-subrc <> 0.
*    WRITE: / icon_red_light AS ICON.

    CALL METHOD l_http_client->get_last_error
      IMPORTING
        code    = l_code
        message = l_message_string.

    CALL METHOD l_http_client->close.

    l_code_string = l_code.
    CONCATENATE 'HTTP-Receive: RC=' l_code_string           "#EC NOTEXT
           INTO l_code_string .
    CONCATENATE l_code_string l_message_string
           INTO l_message_string SEPARATED BY space.

*    PERFORM print_string USING l_message_string.
    p_msg = l_message_string.
    p_subrc = 7.
    EXIT.
  ENDIF.

*-----------------------------------------------------------------------
*- print the results
*-----------------------------------------------------------------------
  CALL METHOD l_http_client->response->get_status
    IMPORTING
      code = l_code.
  IF l_code < 300.
*   HTTP-codes: 100 - 199 = informations
*   HTTP-codes: 200 - 299 = client-request successful (200 = OK)
*    WRITE: / icon_green_light AS ICON.
    p_subrc = 0.
  ELSE.
*   HTTP-codes: 300 - 399 = redirected; further actions required
*   HTTP-codes: 400 - 499 = client-request incomplete
*   HTTP-codes: 500 - 599 = server errors
*    WRITE: / icon_red_light AS ICON.
*    IF l_code BETWEEN 300 AND 399.
*      p_msg = 'Redirected; further actions required'.       "#EC NOTEXT
*    ELSEIF  l_code BETWEEN 400 AND 499.
*      p_msg = 'Client-request incomplete'.                  "#EC NOTEXT
*    ELSE.
*      p_msg = 'Server errors'.                              "#EC NOTEXT
*    ENDIF.
*
*    p_msg = |Web Service: Error { l_code } { p_msg }|.      "#EC NOTEXT

    p_subrc = 6.
  ENDIF.
*-- get the http header fields
*  CALL METHOD l_http_client->response->get_header_fields
*    CHANGING
*      fields = lt_http_fields.
*  LOOP AT lt_http_fields INTO l_http_field_wa.
*    l_char_header = l_http_field_wa-name.
*    WRITE: / l_char_header.
*    l_char_header = l_http_field_wa-value.
*    WRITE: l_char_header.
*  ENDLOOP.
*  WRITE: / sy-uline.

*- get the body
  IF p_subrc = 0.
    l_body_string = l_http_client->response->get_cdata(  ).
*  PERFORM print_string USING l_body_string.
    p_response = l_body_string.
  ELSE.
*-- get the http header fields
    CALL METHOD l_http_client->response->get_header_fields
      CHANGING
        fields = lt_http_fields.
    LOOP AT lt_http_fields INTO l_http_field_wa.
      p_msg = |Webservice: Error { l_http_field_wa-value }|.
      EXIT.
    ENDLOOP.
  ENDIF.

ENDFORM.

Ejemplo con POST

  METHOD http_client.
    TYPE-POOLS: swfxc, icon.
    DATA: l_http_client        TYPE REF TO if_http_client.
    DATA: l_url                TYPE string.
    DATA: l_code               TYPE sy-subrc.
    DATA: l_code_string        TYPE string.
    DATA: l_message_string     TYPE string.
    DATA: lt_http_fields       TYPE tihttpnvp.
    DATA: l_http_field_wa      TYPE ihttpnvp.
    DATA: l_char_header(40)    TYPE c.
    DATA: l_body_string        TYPE string.
*-----------------------------------------------------------------------
*- Create the HTTP-Client
*-----------------------------------------------------------------------

    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = i_url
*       PROXY_HOST         = l_proxy_host
*       PROXY_SERVICE      = l_proxy_service
        ssl_id             = 'ANONYM'
      IMPORTING
        client             = l_http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.
    IF sy-subrc <> 0.
*    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
*            into l_message_string.
      IF l_message_string IS INITIAL.
        l_message_string = 'Error creating HTTP-Client'.    "#EC NOTEXT
      ENDIF.
*    WRITE: / icon_red_light AS ICON.
*    PERFORM print_string USING l_message_string.
      e_msg = l_message_string.
      e_subrc = 9.

      EXIT.
    ENDIF.

*--------------------------------------------------------------------*
* HEADER
*--------------------------------------------------------------------*

*    l_http_client->request->set_method( 'POST' ).
    l_http_client->request->set_method( i_method ).

*    l_http_client->request->set_content_type( 'application/json' ).

    IF i_content_type IS NOT INITIAL.
      l_http_client->request->set_content_type( i_content_type ).
    ENDIF.

    IF i_x_api_key IS SUPPLIED.
      CALL METHOD l_http_client->request->set_header_field
        EXPORTING
          name  = i_x_api_key
          value = i_api_key. "#EC NOTEXT
    ELSE.
      CALL METHOD l_http_client->request->set_header_field
        EXPORTING
          name  = 'X-API-KEY'
          value = i_api_key. "#EC NOTEXT
    ENDIF.

    IF i_content_type IS NOT INITIAL.
      CALL METHOD l_http_client->request->set_header_field
        EXPORTING
          name  = 'Content-Type'
          value = 'application/json'. "#EC NOTEXT
    ENDIF.

*--------------------------------------------------------------------*
* soapenvelope
* string <soapenv:Envelope...</soapenv:Envelope>
*--------------------------------------------------------------------*
    IF i_body IS SUPPLIED.
      CALL METHOD l_http_client->request->set_cdata
        EXPORTING
          data = i_body.
    ENDIF.
*    offset = 0
*    length = '19000'.

** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V
** provide the compression settings to the HTTP client object
**  a) compress request
*  IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true.
*    CALL METHOD l_http_client->set_compression
*      EXPORTING
*        options                  = if_http_client=>co_compress_in_all_cases
*      EXCEPTIONS
*        compression_not_possible = 1.                       "#EC *
** ignore this exception to have the same behaviour as for clients created
** via sm59-destination: for this clients the method set_compression is
** called within the method SEND without exception handling!
*  ENDIF.
*
*
** b) compress response
*  IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true.
*    l_http_client->propertytype_accept_compress = if_http_client=>co_enabled.
*  ELSE.
**    m_client->propertytype_accept_compress = if_http_client=>co_disabled.
*  ENDIF.
*
** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8)
** the HTTP client is not allowed to follow an redirect automatically
** for methods other than HEAD and GET => prevent this for WS calls
** NOTE: prevent the redirect also for PING functionalty although HEAD is
** used in this case in order to get an reliable PING-result
*  l_http_client->propertytype_redirect = if_http_client=>co_disabled.
*-----------------------------------------------------------------------
*- send the http post
*-----------------------------------------------------------------------
    CALL METHOD l_http_client->send
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2.
    IF sy-subrc <> 0.
*    WRITE: / icon_red_light AS ICON.

      CALL METHOD l_http_client->get_last_error
        IMPORTING
          code    = l_code
          message = l_message_string.

      CALL METHOD l_http_client->close.

      l_code_string = l_code.
      CONCATENATE 'HTTP-Send: RC=' l_code_string            "#EC NOTEXT
             INTO l_code_string .
      CONCATENATE l_code_string l_message_string
             INTO l_message_string SEPARATED BY space.

*    PERFORM print_string USING l_message_string.
      e_msg = l_message_string.
      e_subrc = 8.
      EXIT.
    ENDIF.

*-----------------------------------------------------------------------
*- receive the result of http post
*-----------------------------------------------------------------------
    CALL METHOD l_http_client->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2.
    IF sy-subrc <> 0.
*    WRITE: / icon_red_light AS ICON.

      CALL METHOD l_http_client->get_last_error
        IMPORTING
          code    = l_code
          message = l_message_string.

      CALL METHOD l_http_client->close.

      l_code_string = l_code.
      CONCATENATE 'HTTP-Receive: RC=' l_code_string         "#EC NOTEXT
             INTO l_code_string .
      CONCATENATE l_code_string l_message_string
             INTO l_message_string SEPARATED BY space.

*    PERFORM print_string USING l_message_string.
      e_msg = l_message_string.
      e_subrc = 7.
      EXIT.
    ENDIF.

*-----------------------------------------------------------------------
*- print the results
*-----------------------------------------------------------------------
    CALL METHOD l_http_client->response->get_status
      IMPORTING
        code = l_code.
    IF l_code < 300 OR l_code = 400. "bad request
*   HTTP-codes: 100 - 199 = informations
*   HTTP-codes: 200 - 299 = client-request successful (200 = OK)
*    WRITE: / icon_green_light AS ICON.
      e_subrc = 0.
    ELSE.
*   HTTP-codes: 300 - 399 = redirected; further actions required
*   HTTP-codes: 400 - 499 = client-request incomplete
*   HTTP-codes: 500 - 599 = server errors
*    WRITE: / icon_red_light AS ICON.
*    IF l_code BETWEEN 300 AND 399.
*      p_msg = 'Redirected; further actions required'.       "#EC NOTEXT
*    ELSEIF  l_code BETWEEN 400 AND 499.
*      p_msg = 'Client-request incomplete'.                  "#EC NOTEXT
*    ELSE.
*      p_msg = 'Server errors'.                              "#EC NOTEXT
*    ENDIF.
*
*    p_msg = |Cloudsigner: Error { l_code } { p_msg }|.      "#EC NOTEXT

      e_subrc = 6.
    ENDIF.
*-- get the http header fields
*  CALL METHOD l_http_client->response->get_header_fields
*    CHANGING
*      fields = lt_http_fields.
*  LOOP AT lt_http_fields INTO l_http_field_wa.
*    l_char_header = l_http_field_wa-name.
*    WRITE: / l_char_header.
*    l_char_header = l_http_field_wa-value.
*    WRITE: l_char_header.
*  ENDLOOP.
*  WRITE: / sy-uline.

*- get the body
    IF e_subrc = 0.
      l_body_string = l_http_client->response->get_cdata(  ).
*  PERFORM print_string USING l_body_string.
      e_response = l_body_string.
    ELSE.
*-- get the http header fields
      CALL METHOD l_http_client->response->get_header_fields
        CHANGING
          fields = lt_http_fields.
      LOOP AT lt_http_fields INTO l_http_field_wa.
        e_msg = |Acepta: Error { l_http_field_wa-value }|.
        EXIT.
      ENDLOOP.
    ENDIF.
  ENDMETHOD.

Ejemplo con GET y obtener PDF (binario)

  METHOD http_client.
    TYPE-POOLS: swfxc, icon.
    DATA: l_http_client        TYPE REF TO if_http_client.
    DATA: l_url                TYPE string.
    DATA: l_code               TYPE sy-subrc.
    DATA: l_code_string        TYPE string.
    DATA: l_message_string     TYPE string.
    DATA: lt_http_fields       TYPE tihttpnvp.
    DATA: l_http_field_wa      TYPE ihttpnvp.
    DATA: l_char_header(40)    TYPE c.
    DATA: l_body_string        TYPE string.
    DATA: l_body_xstring        TYPE xstring.
*-----------------------------------------------------------------------
*- Create the HTTP-Client
*-----------------------------------------------------------------------
    DATA ssl_id TYPE ssfapplssl.
    ssl_id = 'ANONYM'.
    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = i_url
       ssl_id             = ssl_id  "'ANONYM'
      IMPORTING
        client             = l_http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.
    IF sy-subrc <> 0.
      IF l_message_string IS INITIAL.
        l_message_string = 'Error creating HTTP-Client'.    "#EC NOTEXT
      ENDIF.

      e_msg = l_message_string.
      e_subrc = 9.

      EXIT.
    ENDIF.

*--------------------------------------------------------------------*
* HEADER
*--------------------------------------------------------------------*
    IF i_method IS NOT INITIAL.
      l_http_client->request->set_method( i_method ).
    ENDIF.

    IF i_content_type IS NOT INITIAL.
      l_http_client->request->set_content_type( i_content_type ).
    ENDIF.

    IF i_api_key_key IS NOT INITIAL.
      CALL METHOD l_http_client->request->set_header_field
        EXPORTING
          name  = i_api_key_key
          value = i_api_key_value. "#EC NOTEXT
    ENDIF.

*--------------------------------------------------------------------*
* soapenvelope
* string <soapenv:Envelope...</soapenv:Envelope>
*--------------------------------------------------------------------*
    IF i_body IS SUPPLIED.
      CALL METHOD l_http_client->request->set_cdata
        EXPORTING
          data = i_body.
    ENDIF.

*-----------------------------------------------------------------------
*- send the http post
*-----------------------------------------------------------------------
    CALL METHOD l_http_client->send
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2.
    IF sy-subrc <> 0.
      CALL METHOD l_http_client->get_last_error
        IMPORTING
          code    = l_code
          message = l_message_string.

      CALL METHOD l_http_client->close.

      l_code_string = l_code.
      CONCATENATE 'HTTP-Send: RC=' l_code_string            "#EC NOTEXT
             INTO l_code_string .
      CONCATENATE l_code_string l_message_string
             INTO l_message_string SEPARATED BY space.

      e_msg = l_message_string.
      e_subrc = 8.
      EXIT.
    ENDIF.

*-----------------------------------------------------------------------
*- receive the result of http post
*-----------------------------------------------------------------------
    CALL METHOD l_http_client->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2.
    IF sy-subrc <> 0.

      CALL METHOD l_http_client->get_last_error
        IMPORTING
          code    = l_code
          message = l_message_string.

      CALL METHOD l_http_client->close.

      l_code_string = l_code.
      CONCATENATE 'HTTP-Receive: RC=' l_code_string         "#EC NOTEXT
             INTO l_code_string .
      CONCATENATE l_code_string l_message_string
             INTO l_message_string SEPARATED BY space.

      e_msg = l_message_string.
      e_subrc = 7.
      EXIT.
    ENDIF.

*-----------------------------------------------------------------------
*- print the results
*-----------------------------------------------------------------------
    CALL METHOD l_http_client->response->get_status
      IMPORTING
        code = l_code.
    IF l_code < 300 OR l_code = 400. "bad request
*   HTTP-codes: 100 - 199 = informations
*   HTTP-codes: 200 - 299 = client-request successful (200 = OK)
*    WRITE: / icon_green_light AS ICON.
      e_subrc = 0.
    ELSE.
*   HTTP-codes: 300 - 399 = redirected; further actions required
*   HTTP-codes: 400 - 499 = client-request incomplete
*   HTTP-codes: 500 - 599 = server errors
      e_subrc = 6.
    ENDIF.
    
*- get the body
    IF e_subrc = 0.
      l_body_xstring = l_http_client->response->get_data(  ).
      e_xresponse = l_body_xstring.
    ELSE.
*-- get the http header fields
      CALL METHOD l_http_client->response->get_header_fields
        CHANGING
          fields = lt_http_fields.
      LOOP AT lt_http_fields INTO l_http_field_wa.
        e_msg = |Error { l_http_field_wa-value }|.
        EXIT.
      ENDLOOP.
    ENDIF.
  ENDMETHOD.

  CALL METHOD zcl_obtener_documento=>http_client
    EXPORTING
      i_url       = l_url "https://www.sapwiki.cl/documentos/ObtenerDocumento/Id=12345678'.
*     i_body      =
      i_api_key_key   = l_api_key_key
      i_api_key_value = l_api_key_value
      i_method    = 'GET'
     i_content_type = l_content_type  "space
    IMPORTING
      e_msg       = e_msg
      e_xresponse = e_xdocumento
      e_subrc     = e_subrc.

Llamada a proxy client

* proxy client
  DATA: lo_proxy TYPE REF TO zhcm_co_si_os_carga_documento,
        ls_output TYPE zhcm_mt_carga_documento,
        ls_input  TYPE zhcm_mt_respuesta_documento,
        lo_system_ex TYPE REF TO cx_ai_system_fault,
        lo_app_ex TYPE REF TO cx_ai_application_fault.

*do something
*.....
*

  TRY.
* create proxy
      CREATE OBJECT lo_proxy.

      CALL METHOD lo_proxy->si_os_carga_documento
        EXPORTING
          output = ls_output
        IMPORTING
          input  = ls_input.
    CATCH cx_ai_system_fault  INTO lo_system_ex.
*      WRITE lo_system_ex->errortext TO msg.
*      msg =   lo_system_ex->if_message~get_text( ).
    CATCH cx_ai_application_fault INTO lo_app_ex.
*      WRITE  lo_app_ex->textid TO msg.
*      msg = lo_app_ex->if_message~get_text( ).
  ENDTRY.


  IF lo_system_ex IS NOT INITIAL.
    e_message =   lo_system_ex->if_message~get_text( ).
    e_subrc = 1.
    RETURN.
  ENDIF.

  IF lo_app_ex IS NOT INITIAL.
    e_message =   lo_app_ex->if_message~get_text( ).
    e_subrc = 2.
    RETURN.
  ENDIF.

clase cl_abap_browser=>show_url

DATA: lv_title TYPE cl_abap_browser=>title,
      lv_url   TYPE string.
PARAMETERS p_url TYPE string OBLIGATORY LOWER CASE.

*--------------------------------------------------------------------*
INITIALIZATION.
*--------------------------------------------------------------------*
  p_url = 'https://homer.sii.cl/'.
  lv_title = 'example'.

START-OF-SELECTION.

  CALL METHOD cl_abap_browser=>show_url
    EXPORTING
      url          = p_url
      title        = lv_title
      size         = cl_abap_browser=>xlarge
*     modal        = iv_sapgui_show_in_dialog
      buttons      = cl_abap_browser=>navigate_off
      format       = cl_abap_browser=>landscape
      position     = cl_abap_browser=>topleft
      context_menu = abap_true.

Notas PROXY

  • SRT_UTIL tran. log de llamada proxy
  • Tabla SPROXXSL : relación entre proxy & XSLT hay que concatenar /1SAI/TXS & NAME e ir a la XSLT_TOOL
  • Si en llamada a proxy da error de deserialization, poner break en CL_SXMLP_DATA_ST==============CM004 línea 26
  • RSSIDL_DESERIALIZE_DEMO verificar WSDL, ver nota Ver nota 1801866 - Proxy generation terminated: Incorrect value: Unknown Type Reference
  • Test conexión HTML, reporte RSWF_TEST_HTTP
  • LOG ICM : transacción SMICM, en menú: Pasar a -->Fichero trace-->Visualizar todo
  • Configuración Proxy : transacción SICF
  • Trn. STRUST Instalar certificados
  • Nota 2469949 - "ICF Error when creating HTTP client object by Config for URL" in Web Services ABAP
  • Nota 2334940 - B2A: Troubleshooting HTTPS for communication with health insurance funds
  • Nota 1714792 - Proxy generation terminated: Message must have exactly one part
  • Nota 1817447 - Error "GET_BUSINESS_SYSTEM_ERROR" for Consumer Proxy calls
  • Nota 2033184 - How to create Logical Port manually for a Service Consumer in SOAMANAGER [Video]
  • Nota 1791729 - WDA FileUpload error: "Error when uploading; the file name is invalid or the file is too large"
  • Nota 2322379 - How to create payload trace using SRT_UTIL in AS ABAP
  • Nota 2368112 - Outgoing HTTPS connection does not work in AS ABAP
  • Nota 2388992 - WSDL parsing error: Exception in library handler
  • Nota 2508091 - Web Services ABAP: Error "Element is missing" CX_ST_GROUP_MISSING_CASE
  • Nota 2553979 - SOAP Web Services ABAP - Guided Answers

Varios

clase CL_GUI_HTML_VIEWER método SHOW_URL para ver URL