SAP调用HTTP接口调试

  • Post author:
  • Post category:其他


*&———————————————————————*

*& Report ZTEST2

*&———————————————————————*

*&

*&———————————————————————*

REPORT ZTEST2.


TYPES: BEGIN OF ty_data,

zbytnum   TYPE char30,

zbytvalue TYPE char30,

ztime     TYPE char30,

END OF ty_data.

DATA: lv_url         TYPE string,

lv_response    TYPE string,

lo_http_client TYPE REF TO if_http_client,

lt_data        TYPE TABLE OF ty_data,

lv_json_str    TYPE string,

lv_len         TYPE i.

“—http接口URL

lv_url = url.

“—-创建客户端请求

CALL METHOD cl_http_client=>create_by_url

EXPORTING

url                = lv_url

IMPORTING

client             = lo_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.

CALL METHOD lo_http_client->authenticate(

EXPORTING

*     client   = ”

*     proxy_authentication = ‘X’

username = 账号

password = 密码

*     LANGUAGE = ‘E’

).

“—设置传输请求内容的格式和编码格式

lo_http_client->request->set_content_type( content_type = ‘application/json;charset=utf-8’ ).

“—设置调用服务

lo_http_client->request->set_method( if_http_request=>co_request_method_post ).

“—lv_json_str存放json格式数据

***  CALL METHOD /ui2/cl_json=>serialize

***    EXPORTING

***      data            = 内表数据

***    receiving

***      r_json          = lv_json_str.

“—设置要传输内容

lv_json_str = ‘{ “IMP_ARTICOLI”:[ { ‘.

lv_json_str =  lv_json_str && ‘”ART_ART1″:”888ART00010″, ‘.

lv_json_str =  lv_json_str &&          ‘”ART_DES1″:”DE88SCRIPTION ART00010″, ‘.

lv_json_str =  lv_json_str &&         ‘”ART_UMI1″:”P88Z”  ‘.

lv_json_str =  lv_json_str &&    ‘ }, ‘.

lv_json_str =  lv_json_str &&     ‘ { ‘.

lv_json_str =  lv_json_str &&         ‘”ART_ART”:”ART0088011″, ‘.

lv_json_str =  lv_json_str &&         ‘ “ART_DES”:”DESCRIPTI88ON ART00011″, ‘.

lv_json_str =  lv_json_str &&         ‘”ART_UMI”:”P88Z” ‘.

lv_json_str =  lv_json_str &&     ‘ } ‘.

lv_json_str =  lv_json_str &&  ‘] ‘.

lv_json_str =  lv_json_str && ‘} ‘.

lv_len = strlen( lv_json_str ).

CALL METHOD lo_http_client->request->if_http_entity~set_cdata

EXPORTING

data   = lv_json_str

length = lv_len.

“—发送请求

CALL METHOD lo_http_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.

“—获取失败原因

****    CALL METHOD lo_http_client->get_last_error

****      IMPORTING

****        code           = lv_code

****        message        = lv_message.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH

sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

“—获取http处理结果

CALL METHOD lo_http_client->receive

EXCEPTIONS

http_communication_failure = 1

http_invalid_state         = 2

http_processing_failed     = 3

OTHERS                     = 4.

IF sy-subrc <> 0.

“—获取失败原因

****    CALL METHOD lo_http_client->get_last_error

****      IMPORTING

****        code           = lv_code

****        message        = lv_message.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH

sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

“—读取返回内容

lv_response = lo_http_client->response->get_cdata( ).

CALL METHOD /ui2/cl_json=>deserialize

EXPORTING

json = lv_response

CHANGING

data = lt_data.

“—关闭http连接

CALL METHOD lo_http_client->close.



版权声明:本文为xunlihuabai1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。