本文展示的实例是通过http请求向指定API发送业务数据
following Code:
DATA: lv_url TYPE string. "http 服务接口地址
DATA: lo_http_client TYPE REF TO if_http_client.
DATA: lv_len TYPE i."发送报文长度
DATA: lv_resp TYPE string.
DATA: lv_data TYPE string.
DATA: lv_message TYPE string.
DATA: lv_mtype TYPE bapi_mtype.
DATA: lv_code TYPE sysubrc.
TRY.
" get pdf
PERFORM get_pdf_datas USING iv_edoc_guid
CHANGING lv_data .
" set http url
lv_url = 'https://v0347-iflmap.avtsbhf.eu1.hana.ondemand.com/http/thpdf'.
"create http client
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
"lv_subrc = sy-subrc.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
lo_http_client->authenticate( username = 'username'
password = 'password' ).
" set content type and character set
lo_http_client->request->set_content_type( content_type = 'application/json; charset=base64' ).
lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
"set datas
lv_len = strlen( lv_data ).
CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = lv_data
offset = 0
length = lv_len.
" send request
lo_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2 ).
IF sy-subrc <> 0.
"error
lo_http_client->get_last_error( IMPORTING message = lv_message code = lv_code ).
MESSAGE 'http error' TYPE 'E'.
EXIT.
ENDIF.
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0 .
" lv_subrc = sy-subrc.
* lo_http_client->get_last_error( IMPORTING message = lv_message code = lv_code ).
MESSAGE 'http error' TYPE 'E'.
EXIT.
ELSE.
CLEAR lv_resp.
lv_resp = lo_http_client->response->get_cdata( ).
ENDIF .
CATCH cx_edocument INTO lx_edocument.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDTRY.