抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

之前介绍了如何使用SAP发布供外部调用的HTTP Rest接口,本篇记录一下当别的系统发布了访问的HTTP链接,SAP系统如何调用和进行参数设置。

调用外部HTTP Rest接口

一、调用前准备

在调用别的系统的HTTP接口时,需要了解外围系统的URL与用户名和密码等信息。当然,如果没有设置校验的用户名与密码,则在调用时也不需要考虑这些内容。

本次内容使用网上公开的接口:

本篇笔记主要参考SAP之调用外部接口实例这篇笔记编写的代码。所以本篇笔记主要记录一下调用的流程与代码,然后会在最后把全代码和运行画面展示出来。

二、源码编写

1.选择屏幕输入URL

(1)界面展示

1.选择画面-测试模式 2.选择画面-自定义模式

(2)源码

选择画面源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"Select Run Type                           选择运行类型
SELECTION-SCREEN BEGIN OF BLOCK K01 WITH FRAME TITLE TEXT-T01.
SELECTION-SCREEN BEGIN OF LINE.
"Select Run Type:
PARAMETERS P_TXT NO-DISPLAY.
SELECTION-SCREEN COMMENT 1(26) TEXT-002 FOR FIELD P_TXT.

"Test Mode
SELECTION-SCREEN POSITION 32.
PARAMETERS:
P_TEST RADIOBUTTON GROUP GR01 USER-COMMAND UC1 DEFAULT 'X'. "测试用URL
SELECTION-SCREEN COMMENT 34(27) TEXT-R01 FOR FIELD P_TEST.

"Customize
SELECTION-SCREEN POSITION 62.
PARAMETERS:
P_CUST RADIOBUTTON GROUP GR01. "自定义URL
SELECTION-SCREEN COMMENT 63(32) TEXT-R02 FOR FIELD P_CUST.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK K01.

"Input peripheral interface information 输入外围接口信息
SELECTION-SCREEN BEGIN OF BLOCK K02 WITH FRAME TITLE TEXT-T02.
PARAMETERS:
P_URL TYPE STRING,
P_TYPE TYPE CHAR10 AS LISTBOX VISIBLE LENGTH 10 MODIF ID LIS.
SELECTION-SCREEN END OF BLOCK K02.

"Inquiry Type 查询类型
SELECTION-SCREEN BEGIN OF BLOCK K03 WITH FRAME TITLE TEXT-T03.
"Search for national regions
PARAMETERS:
P_AREA RADIOBUTTON GROUP GR02 USER-COMMAND UC2 DEFAULT 'X'
MODIF ID TES.
"Search for mobile phone location
PARAMETERS:
P_LOCAT RADIOBUTTON GROUP GR02 MODIF ID TES.
PARAMETERS:
P_PHONE TYPE C LENGTH 11 MODIF ID PHO.
SELECTION-SCREEN END OF BLOCK K03.

"Enter account and password
SELECTION-SCREEN BEGIN OF BLOCK K04 WITH FRAME TITLE TEXT-T04.
PARAMETERS:
P_USER TYPE STRING MODIF ID USP,
P_PASS TYPE STRING MODIF ID USP. "和输入框样式一致的类型XUNCODE
SELECTION-SCREEN END OF BLOCK K04.

接口操作类型,选择屏幕下拉框代码。此段逻辑在选择屏幕的AT SELECTION-SCREEN OUTPUT.事件下添加。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
FORM SET_LIST_VALUE .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET',
LC_PUT TYPE STRING VALUE 'PUT',
LC_POST TYPE STRING VALUE 'POST',
LC_PATCH TYPE STRING VALUE 'PATCH',
LC_DELETE TYPE STRING VALUE 'DELETE'.
DATA:
LV_VID TYPE VRM_ID VALUE 'P_TYPE',
LS_LIST TYPE VRM_VALUE,
LT_LIST TYPE VRM_VALUES.
*----------------------------Logic-------------------------------------*
"Set List Value
"Get
LS_LIST-KEY = LC_GET.
LS_LIST-TEXT = 'Get'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Put
LS_LIST-KEY = LC_PUT.
LS_LIST-TEXT = 'Put'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Post
LS_LIST-KEY = LC_POST.
LS_LIST-TEXT = 'Post'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Patch
LS_LIST-KEY = LC_PATCH.
LS_LIST-TEXT = 'Patch'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Delete
LS_LIST-KEY = LC_DELETE.
LS_LIST-TEXT = 'Delete'.
INSERT LS_LIST INTO TABLE LT_LIST.

CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
ID = LV_VID
VALUES = LT_LIST
EXCEPTIONS
ID_ILLEGAL_NAME = 1
OTHERS = 2.

ENDFORM.

2.设置不同模式的URL

(1)界面展示

1.选择画面-测试模式 3.选择画面-测试-手机模式

(2)源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FORM SET_URL_VALUE .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET',
LC_POST TYPE STRING VALUE 'POST'.
*----------------------------Logic-------------------------------------*
CHECK P_TXT IS INITIAL.
IF P_LOCAT IS NOT INITIAL.
"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo
P_URL = TEXT-U02.
P_TYPE = LC_POST.
ELSE.
"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo
P_URL = TEXT-U01.
P_TYPE = LC_GET.
ENDIF.
ENDFORM.

3.创建客户端请求

(1)全局变量

1
2
3
4
5
*----------------------------------------------------------------------*
* Object
*----------------------------------------------------------------------*
DATA:
GO_HTTPCLIENT TYPE REF TO IF_HTTP_CLIENT.

(2)源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"创建客户端请求
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = P_URL
IMPORTING
CLIENT = GO_HTTPCLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
"URL link failed
MESSAGE S000 WITH TEXT-E01 DISPLAY LIKE GC_E.
LEAVE LIST-PROCESSING.
ENDIF.

4.设置用户名与密码

(1)源码

1
2
3
4
5
6
7
8
9
10
11
12
"判断是否需要登陆
IF P_USER IS INITIAL AND
P_PASS IS INITIAL.
"抑制登陆框
GO_HTTPCLIENT->PROPERTYTYPE_LOGON_POPUP = GO_HTTPCLIENT->CO_DISABLED.
ELSE.
"验证用户名与密码
CALL METHOD GO_HTTPCLIENT->AUTHENTICATE
EXPORTING
USERNAME = P_USER
PASSWORD = P_PASS.
ENDIF.

5.设置访问方式与发送函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET',
LC_PUT TYPE STRING VALUE 'PUT',
LC_POST TYPE STRING VALUE 'POST',
LC_PATCH TYPE STRING VALUE 'PATCH',
LC_DELETE TYPE STRING VALUE 'DELETE'.
*----------------------------Logic-------------------------------------*
CASE P_TYPE.
WHEN LC_GET.
"设置请求方式 GET
GO_HTTPCLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET ).
WHEN LC_PUT.
WHEN LC_POST.
"设置传输格式
GO_HTTPCLIENT->REQUEST->SET_CONTENT_TYPE(
CONTENT_TYPE = 'application/x-www-form-urlencoded' ).
"( content_type = 'application/json; charset=utf-8' )
"设置请求方式 POST
GO_HTTPCLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).
"设置JSON内容格式
GV_OUTPUTJSON = 'mobileCode=' && P_PHONE && '&userID='.
WHEN LC_PATCH.
WHEN LC_DELETE.
WHEN OTHERS.
ENDCASE.

6.发送请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"抑制登陆框
GO_HTTPCLIENT->PROPERTYTYPE_LOGON_POPUP = GO_HTTPCLIENT->CO_DISABLED.

"设置版本
GO_HTTPCLIENT->REQUEST->SET_VERSION(
IF_HTTP_ENTITY=>CO_PROTOCOL_VERSION_1_1 ).

"设置传输内容
GO_HTTPCLIENT->REQUEST->SET_CDATA(
DATA = GV_OUTPUTJSON
LENGTH = STRLEN( GV_OUTPUTJSON ) ).

"发送请求
GO_HTTPCLIENT->SEND(
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2 ).
IF SY-SUBRC NE 0.
"获取失败原因
GO_HTTPCLIENT->GET_LAST_ERROR(
IMPORTING
MESSAGE = DATA(LV_MESSAGE) ).

"Call Interface Fail
MESSAGE S000 WITH LV_MESSAGE DISPLAY LIKE GC_E.
LEAVE LIST-PROCESSING.
ENDIF.

7.获取发送请求的返回值

(1)接收返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
*--------------------------Variables-----------------------------------*
DATA:
LV_STATUSCODE TYPE I.
*----------------------------Logic-------------------------------------*
"读取远程服务返回的处理过结果
GO_HTTPCLIENT->RECEIVE(
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3 ).
IF SY-SUBRC NE 0.
"Return Data Error
MESSAGE S000 WITH TEXT-E03 DISPLAY LIKE GC_E.
LEAVE LIST-PROCESSING.
ENDIF.

"读取返回的内容
CLEAR: GV_RETURNJSON.
GV_RETURNJSON = GO_HTTPCLIENT->RESPONSE->GET_CDATA( ).

"将返回参数的回车转换,否则回车会在SAP变成'#'
REPLACE ALL OCCURRENCES OF REGEX '\n' IN GV_RETURNJSON WITH GC_NULL.

"获取运行状态码
GO_HTTPCLIENT->RESPONSE->GET_STATUS(
IMPORTING
CODE = LV_STATUSCODE ).
IF LV_STATUSCODE EQ 200.
CASE GC_X.
WHEN P_AREA.
PERFORM GET_NATIONAL_REGIONAL.
WHEN P_LOCAT.
PERFORM GET_SELECT_PHONE_RESULT.
WHEN OTHERS.
ENDCASE.
ENDIF.

"关闭HTTP请求
CALL METHOD GO_HTTPCLIENT->CLOSE.

(2)展示全国地区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_STRING TYPE STRING VALUE '<string>',
LC_STRING1 TYPE STRING VALUE '</string>'.
*----------------------------Logic-------------------------------------*
SPLIT GV_RETURNJSON AT LC_STRING INTO TABLE DATA(LT_RESPON).
DELETE LT_RESPON INDEX 1.
LOOP AT LT_RESPON INTO DATA(LS_RESPON).
REPLACE LC_STRING1 WITH '' INTO LS_RESPON.
CONDENSE LS_RESPON NO-GAPS.
REPLACE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>CR_LF WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>VERTICAL_TAB WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>NEWLINE WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>FORM_FEED WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>BACKSPACE WITH '' INTO LS_RESPON.
REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
IN LS_RESPON WITH ''
IGNORING CASE.
WRITE: / LS_RESPON.
CLEAR: LS_RESPON.
ENDLOOP.

(3)展示手机归属地

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_SPLITWHERE TYPE STRING VALUE
'<string xmlns="http://WebXml.com.cn/">',
LC_STRING TYPE STRING VALUE '</string>'.
*----------------------------Logic-------------------------------------*
"截取<string xmlns="http://WebXml.com.cn/">右边的值放到内表中
SPLIT GV_RETURNJSON AT LC_SPLITWHERE INTO TABLE DATA(LT_RESPON).
DELETE LT_RESPON INDEX 1.
LOOP AT LT_RESPON INTO DATA(LS_RESPON).
REPLACE LC_STRING WITH '' INTO LS_RESPON.
CONDENSE LS_RESPON NO-GAPS.
REPLACE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>CR_LF WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>VERTICAL_TAB WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>NEWLINE WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>FORM_FEED WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>BACKSPACE WITH '' INTO LS_RESPON.
REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
IN LS_RESPON WITH ''
IGNORING CASE.
WRITE: / LS_RESPON.
CLEAR: LS_RESPON.
ENDLOOP.

三、结果展示

1.全国地区查询

(1)选择画面

1.选择画面-测试模式

(2)结果画面

4.全国地区-结果画面

2.手机号归属地

(1)选择画面

5.选择画面手机号选择

(2)结果画面

6.手机号归属地结果画面

四、全部代码

1.主代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
REPORT YCALLRESTHTTP MESSAGE-ID ZMESCLA_CYH.

*----------------------------------------------------------------------*
* Include
*----------------------------------------------------------------------*
INCLUDE YCALLRESTHTTP_TOP.
INCLUDE YCALLRESTHTTP_SCR.
INCLUDE YCALLRESTHTTP_F01.

*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
PERFORM SET_INITIAL_VALUE.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
"Set Dropdown Box Values
PERFORM SET_LIST_VALUE.
"Set Screen Variable Status
PERFORM SET_SCREEN_VARIABLE.
"Set Test Stauts URL And Type
PERFORM SET_URL_VALUE.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
"Call URL
PERFORM CALL_URL.
"Set response logic for call types
PERFORM SET_INTERFACE_TYPE_LOGIC.
"Send Request
PERFORM SEND_REQUEST.
"Obtain peripheral system return value
PERFORM GET_RETURN_DATA.

2.Include TOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
CONSTANTS:
GC_X TYPE C LENGTH 1 VALUE 'X',
GC_S TYPE C LENGTH 1 VALUE 'S',
GC_E TYPE C LENGTH 1 VALUE 'E',
GC_NULL TYPE C LENGTH 1 VALUE ''.

*----------------------------------------------------------------------*
* Variables
*----------------------------------------------------------------------*
DATA:
GV_RETURNJSON TYPE STRING,
GV_OUTPUTJSON TYPE STRING,
GV_MESSAGE TYPE C.

*----------------------------------------------------------------------*
* Object
*----------------------------------------------------------------------*
DATA:
GO_HTTPCLIENT TYPE REF TO IF_HTTP_CLIENT.

3.Include SCR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"Select Run Type                           选择运行类型
SELECTION-SCREEN BEGIN OF BLOCK K01 WITH FRAME TITLE TEXT-T01.
SELECTION-SCREEN BEGIN OF LINE.
"Select Run Type:
PARAMETERS P_TXT NO-DISPLAY.
SELECTION-SCREEN COMMENT 1(26) TEXT-002 FOR FIELD P_TXT.

"Test Mode
SELECTION-SCREEN POSITION 32.
PARAMETERS:
P_TEST RADIOBUTTON GROUP GR01 USER-COMMAND UC1 DEFAULT 'X'. "测试用URL
SELECTION-SCREEN COMMENT 34(27) TEXT-R01 FOR FIELD P_TEST.

"Customize
SELECTION-SCREEN POSITION 62.
PARAMETERS:
P_CUST RADIOBUTTON GROUP GR01. "自定义URL
SELECTION-SCREEN COMMENT 63(32) TEXT-R02 FOR FIELD P_CUST.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK K01.

"Input peripheral interface information 输入外围接口信息
SELECTION-SCREEN BEGIN OF BLOCK K02 WITH FRAME TITLE TEXT-T02.
PARAMETERS:
P_URL TYPE STRING,
P_TYPE TYPE CHAR10 AS LISTBOX VISIBLE LENGTH 10 MODIF ID LIS.
SELECTION-SCREEN END OF BLOCK K02.

"Inquiry Type 查询类型
SELECTION-SCREEN BEGIN OF BLOCK K03 WITH FRAME TITLE TEXT-T03.
"Search for national regions
PARAMETERS:
P_AREA RADIOBUTTON GROUP GR02 USER-COMMAND UC2 DEFAULT 'X'
MODIF ID TES.
"Search for mobile phone location
PARAMETERS:
P_LOCAT RADIOBUTTON GROUP GR02 MODIF ID TES.
PARAMETERS:
P_PHONE TYPE C LENGTH 11 MODIF ID PHO.
SELECTION-SCREEN END OF BLOCK K03.

"Enter account and password
SELECTION-SCREEN BEGIN OF BLOCK K04 WITH FRAME TITLE TEXT-T04.
PARAMETERS:
P_USER TYPE STRING MODIF ID USP,
P_PASS TYPE STRING MODIF ID USP. "和输入框样式一致的类型XUNCODE
SELECTION-SCREEN END OF BLOCK K04.

4.Include F01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
*&---------------------------------------------------------------------*
*& Include YCALLRESTHTTP_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form SET_LIST_VALUE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SET_LIST_VALUE .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET',
LC_PUT TYPE STRING VALUE 'PUT',
LC_POST TYPE STRING VALUE 'POST',
LC_PATCH TYPE STRING VALUE 'PATCH',
LC_DELETE TYPE STRING VALUE 'DELETE'.
DATA:
LV_VID TYPE VRM_ID VALUE 'P_TYPE',
LS_LIST TYPE VRM_VALUE,
LT_LIST TYPE VRM_VALUES.
*----------------------------Logic-------------------------------------*
"Set List Value
"Get
LS_LIST-KEY = LC_GET.
LS_LIST-TEXT = 'Get'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Put
LS_LIST-KEY = LC_PUT.
LS_LIST-TEXT = 'Put'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Post
LS_LIST-KEY = LC_POST.
LS_LIST-TEXT = 'Post'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Patch
LS_LIST-KEY = LC_PATCH.
LS_LIST-TEXT = 'Patch'.
INSERT LS_LIST INTO TABLE LT_LIST.

"Delete
LS_LIST-KEY = LC_DELETE.
LS_LIST-TEXT = 'Delete'.
INSERT LS_LIST INTO TABLE LT_LIST.

CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
ID = LV_VID
VALUES = LT_LIST
EXCEPTIONS
ID_ILLEGAL_NAME = 1
OTHERS = 2.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_INITIAL_VALUE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SET_INITIAL_VALUE .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET'.
*----------------------------Logic-------------------------------------*
"Set Initial Url Value
"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo
P_URL = TEXT-U01.
"Set Call Interface Type Value
P_TYPE = LC_GET.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_SCREEN_VARIABLE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SET_SCREEN_VARIABLE .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_URL TYPE STRING VALUE 'P_URL',
LC_LIST TYPE STRING VALUE 'LIS',
LC_USER TYPE STRING VALUE 'P_USER',
LC_PASS TYPE STRING VALUE 'P_PASS'.
*----------------------------Logic-------------------------------------*
"Set Input URL Not Edit Status
LOOP AT SCREEN.
IF P_TEST EQ GC_X.
IF SCREEN-NAME EQ LC_URL.
SCREEN-INPUT = '0'.
ENDIF.

IF SCREEN-GROUP1 EQ LC_LIST.
SCREEN-ACTIVE = '0'.
ENDIF.

IF SCREEN-GROUP1 EQ 'USP'.
SCREEN-ACTIVE = '0'.
ENDIF.

IF P_AREA IS NOT INITIAL AND
SCREEN-GROUP1 EQ 'PHO'.
SCREEN-ACTIVE = '0'.
ELSEIF P_AREA IS INITIAL AND
SCREEN-GROUP1 EQ 'PHO'.
SCREEN-ACTIVE = '1'.
ENDIF.
ELSE.
IF SCREEN-GROUP1 EQ 'TES' OR
SCREEN-GROUP1 EQ 'PHO'.
SCREEN-ACTIVE = '0'.
ENDIF.
ENDIF.

IF SCREEN-NAME EQ LC_PASS.
SCREEN-INVISIBLE = '1'.
ENDIF.

MODIFY SCREEN.
CLEAR: SCREEN.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CALL_URL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM CALL_URL .
"创建客户端请求
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = P_URL
IMPORTING
CLIENT = GO_HTTPCLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
"URL link failed
MESSAGE S000 WITH TEXT-E01 DISPLAY LIKE GC_E.
LEAVE LIST-PROCESSING.
ENDIF.

"判断是否需要登陆
IF P_USER IS INITIAL AND
P_PASS IS INITIAL.
"抑制登陆框
GO_HTTPCLIENT->PROPERTYTYPE_LOGON_POPUP = GO_HTTPCLIENT->CO_DISABLED.
ELSE.
"验证用户名与密码
CALL METHOD GO_HTTPCLIENT->AUTHENTICATE
EXPORTING
USERNAME = P_USER
PASSWORD = P_PASS.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_INTERFACE_TYPE_LOGIC
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SET_INTERFACE_TYPE_LOGIC .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET',
LC_PUT TYPE STRING VALUE 'PUT',
LC_POST TYPE STRING VALUE 'POST',
LC_PATCH TYPE STRING VALUE 'PATCH',
LC_DELETE TYPE STRING VALUE 'DELETE'.
*----------------------------Logic-------------------------------------*
CASE P_TYPE.
WHEN LC_GET.
GO_HTTPCLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET ).
WHEN LC_PUT.
WHEN LC_POST.
GO_HTTPCLIENT->REQUEST->SET_CONTENT_TYPE(
CONTENT_TYPE = 'application/x-www-form-urlencoded' ).
"( content_type = 'application/json; charset=utf-8' )
GO_HTTPCLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).

GV_OUTPUTJSON = 'mobileCode=' && P_PHONE && '&userID='.
WHEN LC_PATCH.
WHEN LC_DELETE.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SEND_REQUEST
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SEND_REQUEST .
"抑制登陆框
GO_HTTPCLIENT->PROPERTYTYPE_LOGON_POPUP = GO_HTTPCLIENT->CO_DISABLED.

"设置版本
GO_HTTPCLIENT->REQUEST->SET_VERSION(
IF_HTTP_ENTITY=>CO_PROTOCOL_VERSION_1_1 ).

"设置传输内容
GO_HTTPCLIENT->REQUEST->SET_CDATA(
DATA = GV_OUTPUTJSON
LENGTH = STRLEN( GV_OUTPUTJSON ) ).

"发送请求
GO_HTTPCLIENT->SEND(
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2 ).
IF SY-SUBRC NE 0.
"获取失败原因
GO_HTTPCLIENT->GET_LAST_ERROR(
IMPORTING
MESSAGE = DATA(LV_MESSAGE) ).

"Call Interface Fail
MESSAGE S000 WITH LV_MESSAGE DISPLAY LIKE GC_E.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_RETURN_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM GET_RETURN_DATA .
*--------------------------Variables-----------------------------------*
DATA:
LV_STATUSCODE TYPE I.
*----------------------------Logic-------------------------------------*
"读取远程服务返回的处理过结果
GO_HTTPCLIENT->RECEIVE(
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3 ).
IF SY-SUBRC NE 0.
"Return Data Error
MESSAGE S000 WITH TEXT-E03 DISPLAY LIKE GC_E.
LEAVE LIST-PROCESSING.
ENDIF.

"读取返回的内容
CLEAR: GV_RETURNJSON.
GV_RETURNJSON = GO_HTTPCLIENT->RESPONSE->GET_CDATA( ).

"将返回参数的回车转换,否则回车会在SAP变成'#'
REPLACE ALL OCCURRENCES OF REGEX '\n' IN GV_RETURNJSON WITH GC_NULL.

"获取运行状态码
GO_HTTPCLIENT->RESPONSE->GET_STATUS(
IMPORTING
CODE = LV_STATUSCODE ).
IF LV_STATUSCODE EQ 200.
CASE GC_X.
WHEN P_AREA.
PERFORM GET_NATIONAL_REGIONAL.
WHEN P_LOCAT.
PERFORM GET_SELECT_PHONE_RESULT.
WHEN OTHERS.
ENDCASE.
ENDIF.

"关闭HTTP请求
CALL METHOD GO_HTTPCLIENT->CLOSE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_NATIONAL_REGIONAL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM GET_NATIONAL_REGIONAL .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_STRING TYPE STRING VALUE '<string>',
LC_STRING1 TYPE STRING VALUE '</string>'.
*----------------------------Logic-------------------------------------*
SPLIT GV_RETURNJSON AT LC_STRING INTO TABLE DATA(LT_RESPON).
DELETE LT_RESPON INDEX 1.
LOOP AT LT_RESPON INTO DATA(LS_RESPON).
REPLACE LC_STRING1 WITH '' INTO LS_RESPON.
CONDENSE LS_RESPON NO-GAPS.
REPLACE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>CR_LF WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>VERTICAL_TAB WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>NEWLINE WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>FORM_FEED WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>BACKSPACE WITH '' INTO LS_RESPON.
REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
IN LS_RESPON WITH ''
IGNORING CASE.
WRITE: / LS_RESPON.
CLEAR: LS_RESPON.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_SELECT_PHONE_RESULT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM GET_SELECT_PHONE_RESULT .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_SPLITWHERE TYPE STRING VALUE
'<string xmlns="http://WebXml.com.cn/">',
LC_STRING TYPE STRING VALUE '</string>'.
*----------------------------Logic-------------------------------------*
"截取<string xmlns="http://WebXml.com.cn/">右边的值放到内表中
SPLIT GV_RETURNJSON AT LC_SPLITWHERE INTO TABLE DATA(LT_RESPON).
DELETE LT_RESPON INDEX 1.
LOOP AT LT_RESPON INTO DATA(LS_RESPON).
REPLACE LC_STRING WITH '' INTO LS_RESPON.
CONDENSE LS_RESPON NO-GAPS.
REPLACE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>CR_LF WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>VERTICAL_TAB WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>NEWLINE WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>FORM_FEED WITH '' INTO LS_RESPON.
REPLACE CL_ABAP_CHAR_UTILITIES=>BACKSPACE WITH '' INTO LS_RESPON.
REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
IN LS_RESPON WITH ''
IGNORING CASE.
WRITE: / LS_RESPON.
CLEAR: LS_RESPON.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_URL_VALUE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM SET_URL_VALUE .
*--------------------------Variables-----------------------------------*
CONSTANTS:
LC_GET TYPE STRING VALUE 'GET',
LC_POST TYPE STRING VALUE 'POST'.
*----------------------------Logic-------------------------------------*
CHECK P_TXT IS INITIAL.
IF P_LOCAT IS NOT INITIAL.
"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo
P_URL = TEXT-U02.
P_TYPE = LC_POST.
ELSE.
"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo
P_URL = TEXT-U01.
P_TYPE = LC_GET.
ENDIF.
ENDFORM.

五、参考资料

SAP之调用外部接口实例

SAP HTTP调用其他系统接口

ABAP HTTP POST RESTFUL 调用外部接口URL地址

SAP调用外部http url发送接收json格式数据

评论