因為准備要做一個關於調用外部接口的需求,所以自己先練習一下。
程序說明:我已經在.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!
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
