ABAP HTTP POST 记录

代码语言:javascript
复制
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添加信任证书

 HTTPS:

代码语言:javascript
复制
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' ).