方式一:将参数放入head
"HTTP
str_in = '张三'.
"在abap中调用url时,先要对url中的汉字转码ASCII
CALL METHOD cl_http_utility=>escape_url
EXPORTING
unescaped = str_in
RECEIVING
escaped = str_out.
CONCATENATE '/getUserName?name=' str_out INTO LV_PARAM.
call method cl_http_client=>create
exporting
host = 'wthrcdn.etouch.cn'
service = '80'
scheme = '1' "http
proxy_host = wf_proxy
proxy_service = wf_port
importing
client = http_client.
HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED .
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~request_method'
VALUE = 'GET'.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~server_protocol'
VALUE = 'HTTP/1.1'.
"host后的url
call method http_client->request->set_header_field
exporting
name = '~request_uri'
value = LV_PARAM.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'Content-Type'
VALUE = 'application/json'.
CALL METHOD HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2.
CALL METHOD HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* 获取返回的数据
RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
if RETURN_STR is INITIAL.
write: /'用cl_http_client=>create方法:' ,/ 'faild'.
else.
write: /'用cl_http_client=>create方法:' ,/ RETURN_STR.
endif.
CALL METHOD HTTP_CLIENT->CLOSE.
方式二:将参数放入url
CONCATENATE 'http://wthrcdn.etouch.cn/weather_mini'
'?'
'city=' str_out INTO LV_URL.
"URL填全部,带参数
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = HTTP_CLIENT.
HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED .
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~request_method'
VALUE = 'GET'.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~server_protocol'
VALUE = 'HTTP/1.1'.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'Content-Type'
VALUE = 'application/json'. " text/html 看情况填
* "set body
* CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
* EXPORTING
* DATA = LV_PARAM
* OFFSET = 0
* LENGTH = LV_LEN.
CALL METHOD HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2.
CALL METHOD HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* 获取返回的数据
RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
if RETURN_STR is INITIAL.
write: /'用CL_HTTP_CLIENT=>CREATE_BY_URL方法:' ,/ 'faild'.
else.
write: /'用CL_HTTP_CLIENT=>CREATE_BY_URL方法:' ,/ RETURN_STR.
endif.
CALL METHOD HTTP_CLIENT->CLOSE.