ABAP HTTP POST

1.HTTP

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.

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

2.HTTPS

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' ).

https://www.cnblogs.com/xher/p/6590373.html

 

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.

 

posted @ 2018-06-05 10:19  明大叔  阅读(1835)  评论(0编辑  收藏  举报