zoukankan      html  css  js  c++  java
  • sap 调用Http 服务

    REPORT ZMJ_GETAPI.
    
     
    
     
    
    DATA: LEN         TYPE        I, "发送报文长度
    
          LEN_STRING  TYPE        STRING,
    
          URL         TYPE        STRING, "接口地址
    
          HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, "http客户端
    
          POST_STRING TYPE        STRING,
    
          RESULT      TYPE        STRING.
    
    DATA: IT_HEADER  TYPE TIHTTPNVP,
    
          GV_JSON_IN TYPE STRING VALUE '{"IT_INPUT":[{"CUSTOMER":"12345678","PERNR":"00190935"}]}'.
    
    START-OF-SELECTION.
    
      URL = 'http://10.*.*.*:8001/api_sapnew/ZIF_DATA'.
    
      "'http://10.X.X.X:8081/api/test/gettest'.
    
     
    
      "创建http客户端
    
      CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
    
        EXPORTING
    
          URL                = URL
    
        IMPORTING
    
          CLIENT             = HTTP_CLIENT
    
        EXCEPTIONS
    
          ARGUMENT_NOT_FOUND = 1
    
          PLUGIN_NOT_ACTIVE  = 2
    
          INTERNAL_ERROR     = 3
    
          OTHERS             = 4.
    
      "设定传输请求内容格式以及编码格式
    
      HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' ).
    
     
    
      "设置http method 为Get
    
      HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).
    
      "IF_HTTP_ENTITY~CO_REQUEST_METHOD_GET
    
     
    
      "设置待传输内容长度
    
      LEN = STRLEN( GV_JSON_IN ).
    
      "设置调用方法
    
      CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    
        EXPORTING
    
          NAME  = '~request_method'
    
          VALUE = 'POST'.
    
      "设置传入字符串
    
      CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
    
        EXPORTING
    
          DATA   = GV_JSON_IN
    
          OFFSET = 0
    
          LENGTH = LEN.
    
      "发送
    
      CALL METHOD HTTP_CLIENT->SEND
    
        EXCEPTIONS
    
          HTTP_COMMUNICATION_FAILURE = 1
    
          HTTP_INVALID_STATE         = 2
    
          HTTP_PROCESSING_FAILED     = 3
    
          HTTP_INVALID_TIMEOUT       = 4
    
          OTHERS                     = 5.
    
     
    
      "接收
    
      CALL METHOD HTTP_CLIENT->RECEIVE
    
        EXCEPTIONS
    
          HTTP_COMMUNICATION_FAILURE = 1
    
          HTTP_INVALID_STATE         = 2
    
          HTTP_PROCESSING_FAILED     = 3.
    
      "提取返回字符串
    
      RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
    
      "将字符串中的回车符替换,否则abap将会识别为#
    
      REPLACE ALL OCCURRENCES OF REGEX '
    ' IN RESULT WITH SPACE.
    
      "获取接口返回的数据
    
    *  RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
    
     
    
      WRITE: RESULT.
  • 相关阅读:
    两栏自适应布局
    说说bfc 和 HasLayout
    春联式弹动广告
    Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
    2016年新剧《欢乐颂》里面的经典台词
    硬盘怎么保养
    如何保养与维护笔记本硬盘
    仔细看完,你会成为微信高手
    电脑之间用网线直接连接起来快速传送大文件技巧
    阿里云9折优惠码 GQH4IT 使用方法如下:
  • 原文地址:https://www.cnblogs.com/JackeyLove/p/14272470.html
Copyright © 2011-2022 走看看