SAP调用RestfulApi接口接收数据

因为准备要做一个关于调用外部接口的需求,所以自己先练习一下。

程序说明:我已经在.net开发的系统里提供一个api接口,现在在sap访问这个接口,来接收数据。

这里涉及Restful Api知识,以后再分享。

这是一个api地址:http://10.X.X.X:8081/api/test/gettest

首先根据这个url创建一个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 GET METHOD

http_client->request->set_method( if_http_request=>co_request_method_get ).

发送和接收数据

 "发送
  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( ).

  write: result.

输出结果是这样的

跟外部系统返回的数据是一致的。

附上完整代码

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.



start-of-selection.

  url = '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 method 为Get
  http_client->request->set_method( if_http_request=>co_request_method_get ).

  "发送
  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( ).

  write: result.

  

 

以后会继续分享用POST方法发送数据到外部接口的例子。  

作者:明光烁亮
出处:http://www.cnblogs.com/hezhongxun/
微信号:HEme922 欢迎加好友一起交流SAP!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/hezhongxun/p/10736964.html

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值