DATA:pv_http_client TYPE REF TO if_http_client,
pv_url TYPE string.
1.可以通过URL连接或者IP:端口连接,根据实际情况而定,
建议使用URL,省去地址服务器域名转换
"URL接连
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = pv_url
IMPORTING
client = pv_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.
"IP地址链接
CALL METHOD cl_http_client=>create
EXPORTING
host = p_host "IP
service = p_port "端口
-
SCHEME = '1'
-
PROXY_HOST = ''
-
PROXY_SERVICE = '' IMPORTING client = pv_http_client.
CHECK pv_http_client IS NOT INITIAL.
pv_http_client->propertytype_logon_popup = pv_http_client->co_disabled.
"HTTP - > header
CALL METHOD pv_http_client->request->set_header_field
EXPORTING
name = ‘~request_method’
value = ‘POST’.
CALL METHOD pv_http_client->request->set_header_field
EXPORTING
name = ‘~server_protocol’
value = ‘HTTP/1.1’.
"HTTP -> 密码
CALL METHOD pv_http_client->authenticate
EXPORTING
" PROXY_AUTHENTICATION = 'X'
username = gv_account "用户名
password = gv_password."密码
"数据创建及验证
CALL METHOD pv_http_client->request->set_header_field
EXPORTING
name = ‘Content-Type’
value = ‘application/x-www-form-urlencoded’."变量根据实际情况调整
CALL METHOD pv_http_client->request->set_form_field
EXPORTING
name = ‘sip_appkey’
value = ‘www.baidu.com’.
CALL METHOD pv_http_client->request->set_form_field
EXPORTING
name = ‘sip_timestamp’
value = lv_sip_timestamp.
CALL FUNCTION ‘ZFM_GET_MD5’
EXPORTING
iv_string = lv_password
IMPORTING
ev_md5 = lv_password_md5.
lv_md5_all = ‘nea@burgeon.com.cn’ && lv_sip_timestamp && lv_password_md5.
“https://blog.csdn.net/FQHX_116/article/details/126255335?spm=1001.2014.3001.5502 详解MD5加密
CALL FUNCTION ‘ZFM_GET_MD5’”
EXPORTING
iv_string = lv_md5_all
IMPORTING
ev_md5 = lv_md5_all.
CALL METHOD pv_http_client->request->set_form_field
EXPORTING
name = ‘sip_sign’
value = lv_md5_all.
CALL METHOD pv_http_client->request->set_form_field
EXPORTING
name = ‘transactions’
value = pv_datastr.
"JSON传输
CALL METHOD gv_http_client->request->set_header_field
EXPORTING
name = ‘Content-Type’
value = ‘application/json’.
"数据转JSON格式
CALL METHOD cl_fdt_json=>data_to_json
EXPORTING
ia_data = et_data
RECEIVING
rv_json = ev_datastr.
"发送JSON
CALL METHOD gv_http_client->request->set_header_field
EXPORTING
name = ‘~request_uri’
value = gv_url.
"获取JSON长度->也可以不设置
gv_len = strlen( gv_datastr ).
CALL METHOD gv_http_client->request->set_cdata
EXPORTING
data = gv_datastr
offset = 0
length = gv_len.
"数据发送HTTP接口
"发送请求
pv_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2 ).
IF sy-subrc <> 0.
"操作失败,获取失败原因
pv_http_client->get_last_error( IMPORTING message = pv_resultmsg ).
ELSE.
"读取远程服务返回的处理过结果。
pv_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0 .
pv_http_client->get_last_error( IMPORTING message = pv_resultmsg ).
ELSE.
"读取返回返回内容
pv_resultmsg = pv_http_client->response->get_cdata( ).
"获取HTTP返回值
pv_http_client->response->get_status( IMPORTING code = pv_status ).
ENDIF.
ENDIF.
"解析返回JSON
TRY.
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = pv_resultmsg
CHANGING
data = lt_ztdsoms_return.
CATCH cx_sy_move_cast_error.
ENDTRY.
"关闭HTTP链接----重要必须要关闭,不然或导致HTTP请求过多倒是系统DUMP
IF pv_http_client IS NOT INITIAL.
CALL METHOD pv_http_client->close.
ENDIF.
"获取JAVA时间戳----如果需要可以使用
CONVERT DATE sy-datlo TIME sy-timlo
INTO TIME STAMP time TIME ZONE sy-zonlo.
lv_tstmp1 = time.
lv_tstmp2 = ‘19700101000000’.
TRY.
CALL METHOD cl_abap_tstmp=>subtract
EXPORTING
tstmp1 = lv_tstmp1
tstmp2 = lv_tstmp2
RECEIVING
r_secs = lv_secs.
CATCH cx_parameter_invalid_range .
CATCH cx_parameter_invalid_type .
ENDTRY.
lc_secs = lv_secs.文章来源:https://www.toymoban.com/news/detail-410999.html
CONDENSE lc_secs NO-GAPS.文章来源地址https://www.toymoban.com/news/detail-410999.html
到了这里,关于ABAP:调用HTTP接口详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!