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

程序说明:我已经在.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.

输出结果是这样的

8677c2457ea51213059e3f310ed74199.png

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

9fdc38241d45bd1a6882ca03a3629528.png

附上完整代码

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!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐