DATA:lv_http_client TYPE REF TO if_http_client.
"URL接连
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lv_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CHECK lv_http_client IS NOT INITIAL.
lv_http_client->propertytype_logon_popup = lv_http_client->co_disabled.
- SSL的basic 验证-密码
CALL METHOD gv_http_client->authenticate
EXPORTING
" PROXY_AUTHENTICATION = ‘X’
username = gv_account
password = gv_password.
CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = ‘~request_method’
value = ‘POST’.
CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = ‘~server_protocol’
value = ‘HTTP/1.1’.
-
CALL METHOD lv_http_client->request->set_header_field
-
EXPORTING
-
name = 'Content-Type'
-
value = 'application/json'.
CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = ‘Authorization’
value = ‘Basic U2FwOjEyMzQ1Ng==’.CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = ‘Accept’
value = ‘application/json, application/x-www-form-urlencoded’.CALL METHOD lv_http_client->request->set_header_field
EXPORTING
name = ‘Content-Type’
value = ‘application/x-www-form-urlencoded’."FORM-DATA
CALL METHOD lv_http_client->request->set_form_field
EXPORTING
name = ‘grant_type’
value = ‘client_credentials’.CALL METHOD lv_http_client->request->set_form_field
EXPORTING
name = ‘client_id’
value = ‘Sap’.CALL METHOD lv_http_client->request->set_form_field
EXPORTING
name = ‘client_secret’
value = ‘$2a 10 10 10UByUvns5kgBFDbH2FMK0aOqq5yDYEXshfMxPJCoq85XMlc0ZeYuzW’."发送请求
lv_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2 ).IF sy-subrc <> 0.
"操作失败,获取失败原因
lv_http_client->get_last_error( IMPORTING message = ev_resultmsg ).ELSE.
"读取远程服务返回的处理过结果。
lv_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0 .
lv_http_client->get_last_error( IMPORTING message = ev_resultmsg ).
ELSE.
"读取返回返回内容
ev_resultmsg = lv_http_client->response->get_cdata( ).
"获取HTTP返回值
lv_http_client->response->get_status( IMPORTING code = ev_status ).
ENDIF.ENDIF.
IF lv_http_client IS NOT INITIAL.
CALL METHOD lv_http_client->close.
ENDIF.