June3

HTTP Client

    DATA:      
    lo_http_client TYPE REF TO if_http_client,      
    lo_rest_client TYPE REF TO cl_rest_http_client,      
    lo_response    TYPE REF TO if_rest_entity,      
    lo_request     TYPE REF TO if_rest_entity.
    
    TRY.
        cl_http_client=>create_by_url(
                          EXPORTING url     = me->url
                          IMPORTING client  = lo_http_client ).
      CATCH cx_root.
        RAISE EXCEPTION TYPE zcx_net100_intf_error
          MESSAGE ID me->msgid NUMBER 009.
    ENDTRY.
    lv_username = me->srvinfo-username.
    lv_password = me->srvinfo-password.
    lo_http_client->authenticate( username = lv_username
                                  password = lv_password ).
    lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.
    IF lo_http_client IS NOT BOUND.
      RAISE EXCEPTION TYPE zcx_net100_intf_error
        MESSAGE ID me->msgid NUMBER 012.
    ENDIF.
    CREATE OBJECT lo_rest_client
      EXPORTING
        io_http_client = lo_http_client.
    IF lo_rest_client IS NOT BOUND.
      RAISE EXCEPTION TYPE zcx_net100_intf_error
        MESSAGE ID me->msgid NUMBER 012.
    ENDIF.
    lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
    lo_request->set_content_type( 
                  iv_media_type = if_rest_media_type=>gc_text_plain ).
    lo_request->set_header_field( iv_name = 'Accept-Language' iv_value = 'en-us' ).
    lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
*   GET HTTP RESP STATUS CODE
    lv_status   = lo_response->get_header_field( '~status_code' ).
    IF lv_status = '200'.
*     GET PAYLOAD WHEN SUCCESS
      ls_8102-werks   = me->werks.
      ls_8102-payload = lo_response->get_string_data( ).
      me->trcid = ls_8101-trcid.
    ENDIF.

本篇文章已有0条评论