SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

HTTP



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Function Modules | Функциональные модули
View previous topic :: View next topic  
Author Message
Armann
Модератор
Модератор



Joined: 01 Jan 2008
Posts: 422
Location: Moscow

PostPosted: Fri Dec 04, 2009 6:01 pm    Post subject: HTTP Reply with quote

ФМ HTTP2_GET
У этого ФМа есть один нюанс - в таблице response_entity_body возвращается не текст, как положено, а некое двоичное представление этого текста, которое еще нужно привести в удобоваримый вид.
Нижеследующий исходник, любезно предоставленный John Doe, демонстрирует как это сделать:
Code:

TYPE-POOLS: truxs, slis.

CONSTANTS: cs_url TYPE mmpur_url
VALUE 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=DD/MM/YYYY&d='.
DATA: url TYPE mmpur_url,
      length TYPE i,
      http_load_data TYPE truxs_xml_table,
      http_load_headers TYPE TABLE OF char256,
      xml_in TYPE xstring,
      xml_out TYPE TABLE OF smum_xmltb,
      xml_line TYPE smum_xmltb,
      return TYPE TABLE OF bapiret2,
      fieldname TYPE fieldname,
      fcat TYPE slis_t_fieldcat_alv,
      title TYPE text70.

DATA: BEGIN OF itab OCCURS 0,
        id       LIKE vwrpltaf-feldname,
        numcode  LIKE t242q-numbr,
        charcode LIKE tcurr-tcurr,
        nominal  LIKE tcurr-tfact,
        name     LIKE tcurt-ltext,
        value    LIKE tcurr-ukurs,
      END OF itab.

FIELD-SYMBOLS: <field>.

PARAMETERS: datum TYPE sy-datum DEFAULT sy-datum,
            daily RADIOBUTTON GROUP type DEFAULT 'X',
            montly RADIOBUTTON GROUP type.

START-OF-SELECTION.
  url = cs_url.
  REPLACE ALL OCCURRENCES OF:
    'DD' IN url WITH datum+6(2),
    'MM' IN url WITH datum+4(2),
    'YYYY' IN url WITH datum(4).

  IF daily IS NOT INITIAL.
    CONCATENATE url '0' INTO url.
  ELSE.
    CONCATENATE url '1' INTO url.
  ENDIF.

  CALL FUNCTION 'HTTP2_GET'
    EXPORTING
      absolute_uri                = url
    IMPORTING
      response_entity_body_length = length
    TABLES
      response_entity_body        = http_load_data
      response_headers            = http_load_headers
    EXCEPTIONS
      OTHERS                      = 1.

END-OF-SELECTION.
  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = length
    IMPORTING
      buffer       = xml_in
    TABLES
      binary_tab   = http_load_data
    EXCEPTIONS
      OTHERS       = 1.

  CALL FUNCTION 'SMUM_XML_PARSE'
    EXPORTING
      xml_input = xml_in
    TABLES
      xml_table = xml_out
      return    = return.


  READ TABLE return TRANSPORTING NO FIELDS
    WITH KEY type = 'E'.
  IF sy-subrc IS INITIAL.
    CALL FUNCTION 'C14ALD_BAPIRET2_SHOW'
      TABLES
        i_bapiret2_tab = return.
    EXIT.
  ENDIF.

  LOOP AT xml_out INTO xml_line.
    TRANSLATE xml_line-cname TO UPPER CASE.
    ASSIGN COMPONENT xml_line-cname OF STRUCTURE itab TO <field>.
    CHECK sy-subrc IS INITIAL.

    IF xml_line-cname EQ 'VALUE'.
      REPLACE FIRST OCCURRENCE OF ',' IN xml_line-cvalue WITH '.'.
      MOVE xml_line-cvalue TO <field>.
      APPEND itab.
    ELSEIF xml_line-cname EQ 'VALUTE'.
      CLEAR itab.
      CONTINUE.
    ELSE.
      MOVE xml_line-cvalue TO <field>.
    ENDIF.
  ENDLOOP.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-repid
      i_internal_tabname     = 'ITAB'
      i_inclname             = sy-repid
    CHANGING
      ct_fieldcat            = fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  WRITE datum TO title.
  CONCATENATE 'Currency rates from CBRF on' title INTO
    title SEPARATED BY space.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat  = fcat
      i_grid_title = title
    TABLES
      t_outtab     = itab.



Добавил John Doe: это пример по считыванию курсов валют с сайта ЦБ РФ. Выложил полностью для увеличения информативности. В данной программе присутствует также привязка к типам таблицы данных.
Back to top
View user's profile Send private message Blog
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Function Modules | Функциональные модули All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.