HTTP Client Code



*&---------------------------------------------------------------------*
*& Report  RSWF_TEST_HTTP                                              *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

report  zrswf_test_http                          .

*- begin of internal data
type-pools: swfxc, icon.
data: l_http_client        type ref to if_http_client.
data: l_url                type string.
data: l_code               type sy-subrc.
data: l_code_string        type string.
data: l_message_string     type string.
data: lt_http_fields       type tihttpnvp.
data: l_http_field_wa      type ihttpnvp.
data: l_char_header(40)    type c.
data: l_body_string        type string.
*- end   of internal data

*-----------------------------------------------------------------------
*- Create the HTTP-Client
*-----------------------------------------------------------------------

  call method cl_http_client=>create_by_url
    exporting
      url                = l_url
*     PROXY_HOST         = l_proxy_host
*     PROXY_SERVICE      = l_proxy_service
    importing
      client             = l_http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.
  if sy-subrc <> 0.
    exit.
  endif.

  l_http_client->request->set_method( method = if_http_entity=>co_request_method_post ).

*  CALL METHOD l_http_client->request->set_header_field
*    EXPORTING
*      name  = 'Content-Type'
*      value = 'APPLICATION/XML;charset=gbk'.

  data lv_json_data type string.
  l_http_client->request->set_cdata( lv_json_data ).

*-----------------------------------------------------------------------
*- send the http post
*-----------------------------------------------------------------------
  call method l_http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2.
  if sy-subrc <> 0.
    call method l_http_client->get_last_error
      importing
        code    = l_code
        message = l_message_string.

    call method l_http_client->close.
    return.
  endif.

*-----------------------------------------------------------------------
*- receive the result of http post
*-----------------------------------------------------------------------
  call method l_http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2.
  if sy-subrc <> 0.
    call method l_http_client->get_last_error
      importing
        code    = l_code
        message = l_message_string.

    call method l_http_client->close.
    return.
  endif.

*-----------------------------------------------------------------------
*- print the results
*-----------------------------------------------------------------------
  call method l_http_client->response->get_status
    importing
      code = l_code.

*-- get the http header fields
  call method l_http_client->response->get_header_fields
    changing
      fields = lt_http_fields.
  loop at lt_http_fields into l_http_field_wa.
  endloop.

*- get the body
  l_body_string = l_http_client->response->get_cdata(  ).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值