因为准备要做一个关于调用外部接口的需求,所以自己先练习一下。
程序说明:我已经在.net开发的系统里提供一个api接口,现在在sap访问这个接口,来接收数据。
这里涉及Restful Api知识,以后再分享。
这是一个api地址:http://10.X.X.X:8081/api/test/gettest
首先根据这个url创建一个http客户端
1call method cl_http_client=>create_by_url 2 exporting 3 url = url 4 importing 5 client = http_client 6 exceptions 7 argument_not_found = 1 8 plugin_not_active = 2 9 internal_error = 3 10 others = 4.
选择一个HTTP GET METHOD
http_client->request->set_method( if_http_request=>co_request_method_get ).
发送和接收数据
1"发送 2 call method http_client->send 3 exceptions 4 http_communication_failure = 1 5 http_invalid_state = 2 6 http_processing_failed = 3 7 http_invalid_timeout = 4 8 others = 5. 9 10 "接收 11 call method http_client->receive 12 exceptions 13 http_communication_failure = 1 14 http_invalid_state = 2 15 http_processing_failed = 3.
最后就可以获取接收的数据了
1"获取接口返回的数据 2 result = http_client->response->get_cdata( ). 3 4 write: result.
输出结果是这样的

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

附上完整代码
1data: len type i,"发送报文长度 2 len_string type string, 3 url type string, "接口地址 4 http_client type ref to if_http_client,"http客户端 5 post_string type string, 6 result type string. 7 8data: it_header type tihttpnvp. 9 10 11 12start-of-selection. 13 14 url = 'http://10.X.X.X:8081/api/test/gettest'. 15 16 17 "创建http客户端 18 call method cl_http_client=>create_by_url 19 exporting 20 url = url 21 importing 22 client = http_client 23 exceptions 24 argument_not_found = 1 25 plugin_not_active = 2 26 internal_error = 3 27 others = 4. 28 29 "设置http method 为Get 30 http_client->request->set_method( if_http_request=>co_request_method_get ). 31 32 "发送 33 call method http_client->send 34 exceptions 35 http_communication_failure = 1 36 http_invalid_state = 2 37 http_processing_failed = 3 38 http_invalid_timeout = 4 39 others = 5. 40 41 "接收 42 call method http_client->receive 43 exceptions 44 http_communication_failure = 1 45 http_invalid_state = 2 46 http_processing_failed = 3. 47 48 "获取接口返回的数据 49 result = http_client->response->get_cdata( ). 50 51 write: result.
以后会继续分享用POST方法发送数据到外部接口的例子。
作者:明光烁亮
出处:http://www.cnblogs.com/hezhongxun/
微信号:HEme922 欢迎加好友一起交流SAP!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。