ABAP代码如下,仅在内部测试通过,未涉及外部网络WEBAPI及跨域调用。
*&---------------------------------------------------------------------* *& Report ZYC_WEBAPI *&Restful api 测试 REPORT ZYC_WEBAPI. DATA: LEN TYPE I, "发送报文长度 LEN_STRING TYPE STRING, URL TYPE STRING, "接口地址 HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, "http客户端 POST_STRING TYPE STRING, RESULT TYPE STRING. DATA: IT_HEADER TYPE TIHTTPNVP. DATA: serializer TYPE REF TO cl_trex_json_serializer, lv_json TYPE string. DATA: BEGIN OF ls_kv, username TYPE string, password TYPE string, END OF ls_kv. DATA:LT_KV LIKE TABLE OF LS_KV. "LS_KV-username = 'IGKg8elR/1tnTbdVEz92mtV0IwTn7JIYXlFZaoGZv+pQryfg3fJM/ggjAt3s3hqZKakqRe+ULj7Mmu2rhgSxvnK2YM8PQaQqTo
"/urogMBaFEKnalLfWDdIGrE/F9yaaFT3cWP1f/HL6T70PuhJhr4/h4/QH7fiO5+Y+13JOBxMQ='. "ls_kv-password = 'Y57KClQ9AcDEQPgjBIshNFbrFd03dlzdbUn7kLyJ4tTjoRDuqAZ2Zj6mbX505bw+uO0148OQrGOWEnlYEAgM+gbs244UagqXn3NkH
"/y8v3NW6J0ZkoiZBd1JwHLf/AA33fD4aUjkd7JTZaZw3a5a4Dgpbyk6x3iJzSQKGSlaL9I='. *--------------------------------------------------------------------* url = 'http://10.0.31.154/WebApi/api/values/'. ls_kv-username = 'Tom'. ls_kv-password = 'pwd'. CREATE OBJECT serializer EXPORTING data = ls_kv. *--------------------------------------------------------------------* *url = 'http://10.0.31.154/WebApi/api/sap/'. *ls_kv-username = 'Tom'. *ls_kv-password = 'pwd'. *APPEND ls_kv TO LT_KV. *ls_kv-username = 'Tom1'. *ls_kv-password = 'pwd1'. *APPEND ls_kv TO LT_KV. *CREATE OBJECT serializer * EXPORTING * data = LT_KV. *--------------------------------------------------------------------* serializer->serialize( ). lv_json = serializer->get_data( ). START-OF-SELECTION. "创建http客户端 CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL EXPORTING URL = URL IMPORTING CLIENT = HTTP_CLIENT EXCEPTIONS ARGUMENT_NOT_FOUND = 1 PLUGIN_NOT_ACTIVE = 2 INTERNAL_ERROR = 3 OTHERS = 4. "设定传输请求内容格式以及编码格式 HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' ). "设置http method 为Post "HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ). "HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET ). "IF_HTTP_ENTITY~CO_REQUEST_METHOD_GET * "设置待传输内容长度 LEN = STRLEN( lv_json ). "设置调用方法 * CALL METHOD HTTP_CLIENT->REQUEST->SET_header_FIELD * EXPORTING * NAME = 'sign' * VALUE = 'nL4P52yTnkiT7pV5ZxpA0t4/d8iD02/SrWrXJM4Cktk='. "设置传入字符串 CALL METHOD HTTP_CLIENT->REQUEST->SET_METHOD( 'POST' ). CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA EXPORTING DATA = lv_json OFFSET = 0 LENGTH = LEN. "发送 CALL METHOD HTTP_CLIENT->SEND EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2 HTTP_PROCESSING_FAILED = 3 HTTP_INVALID_TIMEOUT = 4 OTHERS = 5. "接收 CALL METHOD HTTP_CLIENT->RECEIVE EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2 HTTP_PROCESSING_FAILED = 3. IF sy-subrc = 0. ENDIF. "提取返回字符串 RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ). "将字符串中的回车符替换,否则abap将会识别为# REPLACE ALL OCCURRENCES OF REGEX '\n' IN RESULT WITH ''. "获取接口返回的数据 * RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ). WRITE: RESULT.
标签:WEBAPI,调用,HTTP,ABAP,CLIENT,ls,TYPE,METHOD,kv From: https://www.cnblogs.com/kang2008/p/18165453