主要是CPI要用到WSDL文件做mapping,客户的SAP服务器不一定直接可在浏览器访问http或者https的地址,所以在SAP里面开发程序内部调用地址获取WSDL文件
*&---------------------------------------------------------------------*
*& Report YXX_GET_WSDL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT yxx_get_wsdl.
DATA: gv_url TYPE string,
gv_protocol TYPE string,
gv_host TYPE rfcdisplay-rfchost,
gv_ip TYPE rfcdisplay-rfchost,
gv_port TYPE string,
gv_resp TYPE string,
gv_path TYPE string,
gv_fullpath TYPE string,
gv_extension TYPE string,
gv_filename TYPE string,
go_http_client TYPE REF TO if_http_client,
gt_resp TYPE TABLE OF string,
gt_service_info TYPE TABLE OF icm_sinfo2.
SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME TITLE TEXT-101.
PARAMETERS: p_rfc TYPE rs38l-name OBLIGATORY MATCHCODE OBJECT sfunc_modules.
SELECTION-SCREEN END OF BLOCK blk01.
START-OF-SELECTION.
" 检查函数名
SELECT SINGLE
funcname
FROM tfdir
WHERE
funcname = @p_rfc
INTO @DATA(ls_tfdir).
IF sy-subrc <> 0.
MESSAGE '函数不存在' TYPE 'E'.
ENDIF.
" 获取ip
gv_host = sy-host.
CALL FUNCTION 'RFC_HOST_TO_IP'
EXPORTING
rfchost = gv_host
IMPORTING
rfcip = gv_ip.
" 获取http端口
CALL FUNCTION 'ICM_GET_INFO2'
TABLES
servlist = gt_service_info.
gv_port = VALUE #( gt_service_info[ protocol = 1 ]-service OPTIONAL ). " HTTP 端口
gv_protocol = 'http'.
IF gv_port IS INITIAL.
gv_port = VALUE #( gt_service_info[ protocol = 2 ]-service OPTIONAL ). " HTTPS 端口
gv_protocol = 'https'.
ENDIF.
" WSDL地址 使用之前需要在SICF Service Tree中激活WSDL服务的地址才可以访问
gv_url = |{ gv_protocol }://{ gv_ip }:{ gv_port }/sap/bc/soap/wsdl11?services={ p_rfc }&sap-client={ sy-mandt }|.
" 调用WSDL地址
cl_http_client=>create_by_url(
EXPORTING
url = gv_url
IMPORTING
client = go_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.
EXIT.
ENDIF.
" 设置授权
go_http_client->request->set_content_type( content_type = 'application/xml; charset=utf-8' ).
go_http_client->request->set_authorization( username = CONV #( sy-uname ) password = CONV string( '' ) ).
go_http_client->authenticate( username = CONV #( sy-uname ) password = CONV string( '' ) ).
go_http_client->request->set_method( if_http_request=>co_request_method_post ).
" 发送请求
go_http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2 ).
" 读取远程服务返回的结果消息。
go_http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0.
"操作失败,获取失败原因
go_http_client->get_last_error( IMPORTING message = DATA(lv_message) ).
MESSAGE lv_message TYPE 'E'.
ELSE.
gv_resp = go_http_client->response->get_cdata( ).
" 选择文件保存路径
cl_gui_frontend_services=>file_save_dialog(
EXPORTING
default_extension = 'WSDL'
default_file_name = CONV string( p_rfc )
CHANGING
filename = gv_filename
path = gv_path
fullpath = gv_fullpath
EXCEPTIONS
OTHERS = 1 ).
IF sy-subrc <> 0 OR gv_fullpath IS INITIAL.
MESSAGE '未选择文件保存路径' TYPE 'E'. " 未选择文件保存路径
RETURN.
ENDIF.
APPEND gv_resp TO gt_resp.
" 下载本地
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = gv_fullpath
filetype = 'DAT'
TABLES
data_tab = gt_resp.
ENDIF.