ABAP:调用HTTP接口详解

这篇具有很好参考价值的文章主要介绍了ABAP:调用HTTP接口详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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
ABAP:调用HTTP接口详解

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 -> 密码
ABAP:调用HTTP接口详解

CALL METHOD pv_http_client->authenticate
  EXPORTING
    "  PROXY_AUTHENTICATION = 'X'
    username = gv_account    "用户名
    password = gv_password."密码

"数据创建及验证
ABAP:调用HTTP接口详解
ABAP:调用HTTP接口详解

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.

ABAP:调用HTTP接口详解
ABAP:调用HTTP接口详解

"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.

CONDENSE lc_secs NO-GAPS.文章来源地址https://www.toymoban.com/news/detail-410999.html

到了这里,关于ABAP:调用HTTP接口详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包