1, 普通
FUNCTION zfm_test_url.
*"----------------------------------------------------------------------
*"*"本地接口:
*"----------------------------------------------------------------------
DATA: http_client TYPE REF TO if_http_client,
loc_conv TYPE REF TO cl_abap_conv_in_ce,
l_url TYPE string,
l_xstr TYPE xstring,
l_str TYPE string,
l_result TYPE string.
DATA: result_tab TYPE TABLE OF string WITH HEADER LINE.
l_url = '' .
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = l_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
** Set request method to GET or POST
http_client->request->set_method( if_http_request=>co_request_method_post ).
CALL METHOD http_client->request->set_header_field
EXPORTING
name = 'content'
value = 'test'.
CALL METHOD http_client->request->set_header_field
EXPORTING
name = 'target'
value = '18961701938'.
CALL METHOD http_client->request->set_header_field
EXPORTING
name = 'Content-Type'
value = 'text/html;charset=utf-8'. "发送字符串时只能用utf-8编码
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.
CLEAR l_result .
l_xstr = http_client->response->get_data( ).
IF sy-subrc = 0 .
http_client->close( ).
ENDIF.
CALL METHOD cl_abap_conv_in_ce=>create
EXPORTING
input = l_xstr
encoding = 'UTF-8'
replacement = '?'
ignore_cerr = abap_true
RECEIVING
conv = loc_conv.
TRY.
CALL METHOD loc_conv->read
IMPORTING
data = l_str.
CATCH cx_sy_conversion_codepage.
CATCH cx_sy_codepage_converter_init.
CATCH cx_parameter_invalid_type.
CATCH cx_parameter_invalid_range.
ENDTRY.
* SPLIT l_str AT cl_abap_char_utilities=>cr_lf INTO TABLE result_tab .
SPLIT l_str AT cl_abap_char_utilities=>newline INTO TABLE result_tab .
ENDFUNCTION.
2 json
FUNCTION zfm_test_url5.
*"----------------------------------------------------------------------
*"*"本地接口:
*"----------------------------------------------------------------------
*
DATA: http_client TYPE REF TO if_http_client,
loc_conv TYPE REF TO cl_abap_conv_in_ce,
l_url TYPE string,
l_xstr TYPE xstring,
l_str TYPE string,
lv_content TYPE string,
lv_type TYPE string,
lv_target TYPE string,
lv_error_msg TYPE string,
l_result TYPE string.
DATA lv_len TYPE i .
lv_content = '25' .
lv_type = '17' .
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id
SOURCE f1 = lv_content
f2 = lv_type
RESULT XML json_writer.
DATA(json) = json_writer->get_output( ).
DATA: lv_json TYPE string .
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = json
im_encoding = 'UTF-8'
IMPORTING
ex_string = lv_json.
CONCATENATE 'data=' lv_json INTO lv_json .
l_url = ''.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = l_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
**content type 非常重要
http_client->request->set_content_type( content_type = 'application/x-www-form-urlencoded; charset=utf-8' ).
http_client->request->set_method( if_http_request=>co_request_method_post ).
lv_len = strlen( lv_json ).
CALL METHOD http_client->request->set_cdata
EXPORTING
data = lv_json
offset = 0
length = lv_len.
http_client->send( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2 ).
IF sy-subrc NE 0.
http_client->get_last_error( IMPORTING message = lv_error_msg ).
ENDIF.
*Receive the Response Object
http_client->receive( EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
IF sy-subrc <> 0 .
http_client->get_last_error( IMPORTING message = lv_error_msg ).
ENDIF.
DATA lv_body TYPE string.
lv_body = http_client->response->get_cdata( ).
http_client->close( ).
* CALL TRANSFORMATION id
* SOURCE XML lv_json
* RESULT content = lv_content1
* type = lv_type1
* target = lv_target1.
ENDFUNCTION.
阅读(2532) | 评论(0) | 转发(0) |