zoukankan      html  css  js  c++  java
  • ABAP-HTTP发送JSON

    1. HTTP发送JSON格式数据:

    function zap_01_url_sap_to_c3 .
    *"----------------------------------------------------------------------
    *"*"本地接口:
    *"  IMPORTING
    *"     VALUE(I_FLAG) TYPE  STRING OPTIONAL
    *"     VALUE(I_TITLE) TYPE  STRING
    *"     VALUE(I_MESSAGE) TYPE  STRING
    *"  EXPORTING
    *"     VALUE(E_RETURN) TYPE  STRING
    *"  TABLES
    *"      IT_REVEIVER
    *"----------------------------------------------------------------------
    
      data: lv_url type string."http 服务接口地址
      data: lo_http_client  type ref to if_http_client.
      data: lv_respon type string.
      data: lv_subrc type sy-subrc.
      data: lv_json_str type string. "发送报文
      data: lv_msgty type c,
            lv_msgtx type string.
    
      data: lt_json type standard table of zscma_dyjg,
            ls_json type zscma_dyjg.
    
    * 设置报文内容
      ls_json-source        = 'ERP' . "来源 订阅号的英文名
      ls_json-sender        = 'ERP' .  "发送者
      ls_json-sender_type   = 'S' . "默认个人  P为个人  S为系统
    
      data:lv_reveiver type string.
    
      loop at it_reveiver.
        if sy-tabix eq 1.
          lv_reveiver = it_reveiver.
        else.
          lv_reveiver = lv_reveiver && ',' && it_reveiver.
        endif.
      endloop.
    
      ls_json-title    = i_title . "发送标题
      ls_json-content  = i_message.
      ls_json-message  = 'message'. "
      ls_json-channel  = 'SUBSCRIPTION'. "
      ls_json-messagedata = i_message. "
    
      if i_flag is initial.
    
        concatenate 'http://XXX.XXX.XXX.XXX:XXXX/mc/core/message/sendMessageUnitForCode?source=' ls_json-source
                    '&sender='ls_json-source'&sender_type=S&receiver='lv_reveiver'&title='ls_json-title
                    '&content='ls_json-content'&message='ls_json-message
                    '&channel=SUBSCRIPTION&messageData=%7B%22type%22%3A%2210002%22%2C%22createTime%22%3A%22%22%2C%22remarkUrl%22%3A%22http%3A%2F%2Fwww.baidu.com%22%7D'
                    into lv_url .
    
    *    /mc/core/message/sendMessageUnitForCode
    *    /mc/core/message/sendMessageUnit
    
      elseif i_flag eq 'X'.
        concatenate 'http://XXX.XXX.XXX.XXX:XXXX/mc/core/message/sendMessageUnitForCode?source=' ls_json-source
                  '&sender='ls_json-source'&sender_type=S&receiver='lv_reveiver'&title='ls_json-title
                  '&content='ls_json-content'&message='ls_json-message
                  '&channel=SUBSCRIPTION&messageData=%7B%22type%22%3A%2210002%22%2C%22createTime%22%3A%22%22%2C%22remarkUrl%22%3A%22http%3A%2F%2Fwww.baidu.com%22%7D'
                  into lv_url .
      endif.
    
      "创建客户端请求
      call method cl_http_client=>create_by_url
        exporting
          url                = lv_url
        importing
          client             = lo_http_client
        exceptions
          argument_not_found = 1
          plugin_not_active  = 2
          internal_error     = 3
          others             = 4.
    
    
      "设定传输请求内容格式以及编码格式
      lo_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
    
      "设定调用服务
      lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
    
      "发送请求
      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 ).
    
      e_return = lo_http_client->response->get_cdata( ).
    
      e_return = e_return+9(1).
    
      translate e_return to upper case.
    
    endfunction.

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

     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' ).
  • 相关阅读:
    IList扩展
    WPF 路由事件
    WPF 属性值绑定、转换
    WPF 生命周期
    App.config 动态编辑
    Oracle Package的全局变量与Session
    AES对数据进行加密与解密
    OracleAES加密
    AES加解密程序的实现
    Oracle的AES加密与解密用法
  • 原文地址:https://www.cnblogs.com/ricoo/p/10131407.html
Copyright © 2011-2022 走看看