matinal:SAP ABAP http集成的几种不同场景举例

本文介绍了如何在Java中使用httplib进行SAP系统HTTPPOST请求,涉及HTTPS连接、ODATA服务调用及XML数据构造,强调了认证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

某xx系统别来网上找文章了,你说你一个做java,非要到网上找SAP文章,给做SAP的人看,我觉得你这样的人先看看自己的问题。

1. http post方式发送数据给对方系统

DATA: lo_http_client   TYPE REF TO if_http_client,
      lv_service       TYPE string,
      lv_result        TYPE string,
      lo_ixml          TYPE REF TO if_ixml,
      lo_streamfactory TYPE REF TO if_ixml_stream_factory,
      lo_istream       TYPE REF TO if_ixml_istream,
      lo_document      TYPE REF TO if_ixml_document,
      lo_parser        TYPE REF TO if_ixml_parser.

lv_service = 'http://...'.

cl_http_client=>create_by_url(
  EXPORTING
    url                = lv_service
  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4 ).

lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
CALL METHOD lo_http_client->authenticate(
  EXPORTING
*   client   = ''
*   proxy_authentication = 'X'
    username = ''
    password = ''
*   LANGUAGE = 'E'
               ).
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Content-Type'
    value = 'application/JSON; charset=utf-8'.
CALL METHOD lo_http_client->request->set_method( 'POST' ).

DATA lv_json TYPE string.
DATA: len TYPE i .
lv_json =  '{"key": "name", "value": "name"}, {"key": "phone", "value": "911"}'.
len = strlen( lv_json ) .

CALL METHOD lo_http_client->request->set_cdata
  EXPORTING
    data   = lv_json
    offset = 0
    length = len.

lo_http_client->send(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2 ).

lo_http_client->receive(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3 ).

CLEAR lv_result .
lv_result = lo_http_client->response->get_cdata( ).
lo_ixml = cl_ixml=>create( ).

lo_streamfactory = lo_ixml->create_stream_factory( ).
lo_istream = lo_streamfactory->create_istream_string(
                                 lv_result ).
lo_document = lo_ixml->create_document( ).
lo_parser = lo_ixml->create_parser(
                       stream_factory = lo_streamfactory
                       istream        = lo_istream
                       document       = lo_document ).
lo_parser->parse( ).
CALL METHOD lo_http_client->close.

 2. https 调用ODATA service,插入数据,事物码STRUST添加信任证书

CALL METHOD cl_http_client=>create
  EXPORTING
    host    = 'api15.sapsf.cn'
    service = '443'                       
    scheme  = '2'                        
    ssl_id  = 'ANONYM'             
*   proxy_host    = wf_proxy
*   proxy_service = wf_port
  IMPORTING
    client  = lo_http_client.

lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
CALL METHOD lo_http_client->authenticate(
  EXPORTING
*   client               = '110'
*    proxy_authentication = 'X'
    username             = ''
    password             = ''
*   LANGUAGE             = 'E'
                           ).
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = '~request_protocol'
    value = 'HTTPS/1.0'.
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = '~request_uri'
    value = '/odata/v2/......'.
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Content-Type'
    value = 'application/json; charset=utf-8'.
CALL METHOD lo_http_client->request->set_method( 'POST' ).

3. SAP 发送 HTTP POST(Web Service)

* 拼接XML的内表
DATA:
  BEGIN OF wareqtext,
    line TYPE c LENGTH 72,
  END OF wareqtext,
  itreqtext LIKE TABLE OF wareqtext.

* URL地址

DATA: l_url TYPE string .

* 拼接的XML
DATA: strreq  TYPE string.

* 返回的XML

DATA: return_str TYPE string .

 

* 生成提交字符串的xml部分
    CLEAR: strreq .
    LOOP AT itreqtext INTO wareqtext.
      IF strreq IS INITIAL .
        strreq = wareqtext.
      ELSE.

* 有些系统不会自动换行,特加上换行符
        CONCATENATE strreq cl_abap_char_utilities=>newline wareqtext-line INTO strreq.
      ENDIF.
    ENDLOOP.

 

  DATA: http_client TYPE REF TO if_http_client .
  DATA: len TYPE i .

  len = STRLEN( strreq ) .

  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url    = l_url
    IMPORTING
      client = http_client.
  http_client->propertytype_logon_popup = http_client->co_enabled .

  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'text/xml; charset=utf-8'.

  CALL METHOD http_client->request->set_cdata
    EXPORTING
      data   = strreq
      offset = 0
      length = len.
  CALL METHOD http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

* 获取返回的数据
  return_str = http_client->response->get_cdata( ).

  CALL METHOD http_client->close.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值