流程图
示例代码
发起GET请求
http://jsonplaceholder.typicode.com/posts
*&---------------------------------------------------------------------*
*& Report ZABAP_HTTP
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZABAP_HTTP.
DATA lv_url TYPE STRING VALUE 'http://jsonplaceholder.typicode.com/posts'.
DATA io_http_client TYPE REF TO IF_HTTP_CLIENT.
"创建http客户端并传入URL
cl_http_client=>create_by_url( EXPORTING url = lv_url IMPORTING client = io_http_client ).
"设置内容格式和编码格式
io_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
" 设置方法为 get
io_http_client->request->set_method( if_http_request=>co_request_method_get ).
"设置传输内容
io_http_client->request->set_cdata( data = 'hello world!' ).
"发送请求
io_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2 ).
if sy-subrc <> 0.
"发送失败,获取失败原因
io_http_client->get_last_error( IMPORTING message = DATA(lv_message) code = DATA(lv_code) ).
WRITE: / '发送失败原因:',lv_message.
exit.
endif.
"读取远程服务器响应
io_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0.
io_http_client->get_last_error( IMPORTING message = DATA(lv_msg) code = DATA(lv_code1) ).
WRITE: / '服务器响应失败原因:',lv_msg.
exit.
EXIT.
ENDIF.
DATA(lv_res) = io_http_client->response->get_cdata( ).
WRITE: / lv_res.
结果
访问发起HTTP GET 请求成功