SAP如何发布Https接口:https://www.cnblogs.com/StephenAmell/p/16744370.html
一、对https接口的简单认识
1.参数-Params
这些参数为必填,看接口的开发文档的要求来就好了。
用户密匙在上面申请创建
2.权限-Authorization
一些API是要用户和密码
3.抬头-Headers
json 格式,和utf-8这类的信息。在ABAP对应的设置是:
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'Content-Type'
VALUE = 'application/json'.
4.主体-Body
为json格式的数据,比如
二、实例操作
1.获取天气接口的密匙
https://lbsyun.baidu.com/apiconsole/center#/home
2.天气接口用这个比较方便
https://tianqiapi.com/
按照网站的提示操作就可以了。
2.SE38创建程序
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 '' .
START-OF-SELECTION.
url = 'https://api.map.baidu.com/weather/v1/district_id=222405&data_type=all&ak=******自己去申请密匙'.
"url = 'http://192.168.******' ."
cl_http_client=>create_by_url(
EXPORTING url = url "服务提供方服务地址
IMPORTING client = DATA(lo_client)
).
"调取方式:get 或者为 post"
" lo_client->request->set_method( if_http_request=>co_request_method_post ).
lo_client->request->set_method( if_http_request=>co_request_method_get ).
** 设置post接口body参数"
lo_client->request->set_cdata( data = gv_json_in ).
** 发送数据
lo_client->send(
* EXPORTING
* timeout = co_timeout_default " Timeout of Answer Waiting Time
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error When Processing Method
http_invalid_timeout = 4 " Invalid Time Entry
OTHERS = 5
).
IF sy-subrc <> 0.
result = '接口接受响应失败'.
ENDIF.
**********************************************************************
** 接收返回参数
lo_client->receive(
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error When Processing Method
OTHERS = 4
).
IF sy-subrc <> 0.
result = '接口接受响应失败'.
ENDIF.
result = lo_client->response->get_cdata( ).
lo_client->close( ).
WRITE: url.
WRITE: / result.
3.上传证书
在网站上下载证书之后,进入STRUST 事务代码,导入证书
3.运行程序
成功返回天气结果
4.https需要导入证书,不然会报500 SSL Peer错误
如果报错404那说明是
1.数据格式或者程序代码问题。
2.另外需要找服务器管理员,在SAP服务器能不能PING 通接口地址,可能需要设置DNS
5.http就不用导入证书就可以链接
url换成内网的IP接口地址
运行结果:接口已经调取成功,只是我传输的内容不对。
参考链接:
SAP调用外部API
我是寒武青锋,分享使我快乐。记得点赞哈