代码语言:javascript复制
变量定义
DATA: lo_client TYPE REF TO if_http_client,
lo_request TYPE REF TO if_http_request,
lv_result TYPE string,
lv_errocode TYPE sysubrc,
lv_message TYPE string,
lv_input TYPE string,
lv_xinput TYPE xstring.
DATA:
lr_json_reader TYPE REF TO if_sxml_reader.
DATA: lv_url TYPE string.
TYPES: BEGIN OF lty_keywords,
key TYPE string,
deleted_at TYPE string,
translations TYPE ty_trans,
END OF lty_keywords.
DATA: lt_result TYPE TABLE OF lty_keywords.
调用JSON request
lv_url = 'http://xx.xxx.0.217/api/keywords'.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc IS NOT INITIAL.
" Error
EXIT.
ENDIF.
lo_client->request->set_header_field( EXPORTING name = '~request_method' value = 'GET' ).
lo_client->authenticate(
EXPORTING
* proxy_authentication = 'X'
username = 'certipedia'
password = 'jfds8201'
* language =
).
CALL METHOD lo_client->send
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.
接受返回值
CALL METHOD lo_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CALL METHOD lo_client->get_last_error
IMPORTING
code = lv_errocode
message = lv_message.
lv_input = lo_client->response->get_cdata( ).
返回数据格式转换
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_input
IMPORTING
buffer = lv_xinput
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
lr_json_reader = cl_sxml_string_reader=>create( input = lv_xinput ).
CALL TRANSFORMATION id SOURCE XML lr_json_reader
RESULT keywords = lt_result.
lo_client->close( ).
第二种方式
代码语言:javascript复制 DATA: lo_client TYPE REF TO if_http_client,
lo_request TYPE REF TO if_http_request,
lv_parameter TYPE string,
lv_parameter2 TYPE string.
DATA: lv_result TYPE string,
lv_errocode TYPE sysubrc,
lv_message TYPE string.
DATA: ls_config TYPE zwzert_id_inf,
lv_username TYPE string,
lv_psw TYPE string,
lv_url TYPE string.
处理参数
lv_parameter = ' { '
& '"id": "=ID=",'
& '"caption": =TMC=, '
& '"layout": 3,'
& '"keywords": [ =KEYWORD=],'
& '"qr_code_url": null,'
& ' "third_party_mark": null,'
& ' "annual_ring_range": null,'
& ' "annual_ring_text": null,'.
lv_parameter2 =
' "output_format": 1,'
& ' "output_quality": 1,'
* & ' "generate_zip": true,'
& ' "generate_zip": false,'
& ' "use_small_font": =FONT=,'
& ' "langs": ['
& ' "=LANG=",'
& ' "=LANG=",'
& '"=LANG="'
* & ' ]'
& ' ],'
& '"generate_jpg_instead_png":true'
& '}'.
lv_length = strlen( lv_parameter ).
CALL FUNCTION 'CONVERT_STRING_TO_TABLE'
EXPORTING
i_string = lv_parameter
i_tabline_length = 80
TABLES
et_table = lt_table.
发送JSON request
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
lo_client->request->set_header_field( EXPORTING name = '~request_method' value = 'POST' ).
lo_client->request->set_header_field( EXPORTING name = '~request_protocol' value = 'HTTP/1.0' ).
lo_client->request->set_header_field( EXPORTING name = 'Content-Type' value = 'text/xml' ).
CALL METHOD lo_client->request->set_header_field
EXPORTING
name = '~request_uri'
value = '/generator/tdcgenerator.svc/generator'.
lo_client->request->set_cdata( EXPORTING data = lv_parameter offset = 0 length = lv_length ).
CALL METHOD lo_client->authenticate
EXPORTING
* proxy_authentication = 'X'
* client =
username = lv_username
password = lv_psw
* language =
.
CALL METHOD lo_client->send
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.
接受反馈信息
CALL METHOD lo_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CALL METHOD lo_client->get_last_error
IMPORTING
code = lv_errocode
message = lv_message.
lv_result = lo_client->response->get_cdata( ).
处理URL
*JPG
FIND REGEX 'http.*jpg' IN lv_result
MATCH OFFSET lv_moff
MATCH LENGTH lv_mlen.
TRY .
ev_url = substring( val = lv_result off = lv_moff len = lv_mlen ).
CATCH cx_sy_range_out_of_bounds.
MESSAGE e151(zw).
ENDTRY.
lo_client->close( ).