Code: Alles auswählen.
String s= "Hello World";
URI uri = URI.create("http://api.example.com/");
HttpRequest request = HttpRequest.newBuilder(uri)
.header("Accept", "application/pdf")
.POST(BodyPublishers.ofString(s))
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse< byte[] > response = client.send(request,BodyHandlers.ofByteArray());
byte[] body = response.body();
if (response.statusCode() == 200) {
File file = new File("label.pdf");
Files.write(file.toPath(), body);
}
Code: Alles auswählen.
* the handler is called whenever a request is made for a particular page
* it is used to restore the internal data structures from the request
DATA ireq TYPE REF TO if_http_request.
DATA ires TYPE REF TO if_http_response.
DATA result TYPE string.
DATA l_mime TYPE string.
ireq = page->get_request( ).
ires = page->get_response( ).
* ? Is it http - POST
DATA l_method TYPE string.
l_method = ireq->get_header_field( '~request_method' ).
IF NOT l_method = 'POST'.
" EXIT.
ENDIF.
* Read all Header fields
DATA headerfields TYPE tihttpnvp.
CALL METHOD ireq->get_header_fields
CHANGING
fields = headerfields.
* Get response content
DATA lo_mr_api TYPE REF TO if_mr_api.
DATA: l_folder TYPE boole_d,
l_mimetype TYPE sdokfilaci-mimetype.
DATA l_xstring TYPE xstring.
* instantiate MIME API class
CALL METHOD cl_mime_repository_api=>if_mr_api~get_api
RECEIVING
r_mr_api = lo_mr_api.
* Get PDF file from MIME repository
CALL METHOD lo_mr_api->get
EXPORTING
i_url = '/SAP/Zhello_world/How-to_ADS.pdf'
IMPORTING
e_is_folder = l_folder
e_content = l_xstring
e_mime_type = l_mimetype
EXCEPTIONS
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
OTHERS = 5.
IF sy-subrc = 0.
l_mime = l_mimetype. " = application/pdf
ires->set_header_field( name = 'content-type' value = l_mime ).
* Send result Body content
ires->set_data( l_xstring ).
ires->set_status( code = 200 reason = 'OK' ).
ELSE.
ires->set_status( code = 400 reason = 'hier stimmt was nicht' ).
ENDIF.
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Report YBC_HTTP_CLIENT_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ybc_http_client_test.
* data declarations
DATA: client TYPE REF TO if_http_client.
DATA:
host TYPE string VALUE 'http://xamp:8081',
errortext TYPE string.
DATA:
uri TYPE string,
subrc TYPE i,
version TYPE i,
request TYPE REF TO if_http_request.
DATA res_data TYPE xstring.
DATA res_cdata TYPE string.
DATA:buffer TYPE xstring,
conv TYPE REF TO cl_abap_conv_in_ce,
buf TYPE TABLE OF tab512 WITH HEADER LINE,
bytes_read TYPE i,
fname TYPE string VALUE 'c:\temp\000.html'.
DEFINE m_err_exit.
if sy-subrc <> 0.
call method client->get_last_error
importing
code = subrc
message = errortext.
exit.
endif.
END-OF-DEFINITION.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = host
IMPORTING
client = client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
m_err_exit.
client->request->set_header_field( name = '~request_method'
value = 'POST' ).
CALL METHOD client->request->set_form_field
EXPORTING
name = '$get'
value = 'checked'.
CALL METHOD client->send
*exporting timeout = timeout
EXCEPTIONS http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
m_err_exit.
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
m_err_exit.
*CALL METHOD client->listen
* RECEIVING
* client = client
* EXCEPTIONS
* http_communication_failure = 1
* http_no_open_connection = 2
* others = 3.
*IF sy-subrc <> 0.
* CALL METHOD client->get_last_error
* IMPORTING
* code = subrc
* message = errortext.
*
* EXIT.
*ENDIF.
*CALL METHOD client->response->get_data
* RECEIVING
* data = res_data.
CALL METHOD client->response->get_cdata
RECEIVING
data = res_cdata.
m_err_exit.
CALL METHOD client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
m_err_exit.
Tron hat geschrieben: ↑19.10.2019 18:13Moin.
Hier mal 2 Beispiele wie Webservices gehostet (SAP = Server) und wie man Webservices aufruft (SAP = Client).
SAP = Server
einen Test Webservice siehe:
viewtopic.php?f=1&t=17609#p60010
Einfach den Inhalt des Zips mal nach programmieren.
Ansonsten kann man so einen Webservice auch einfach über eine BSP-Applikation verwirklichen.
1.) BSP Applikation anlegen und den Inhalt (bis auf erste Zeile) der Startseite löschen.
000.png
2.) Event OnRequest ausprogrammieren
002.png
3.) Ergebnis
003.png
4.) Ein bischen Code zum Testen !!!
Das PDF ist im Mime Repository abgelegt (ggf. die Mime -Url anpassen)
(Method OnRequest)..Code: Alles auswählen.
* the handler is called whenever a request is made for a particular page * it is used to restore the internal data structures from the request DATA ireq TYPE REF TO if_http_request. DATA ires TYPE REF TO if_http_response. DATA result TYPE string. DATA l_mime TYPE string. ireq = page->get_request( ). ires = page->get_response( ). * ? Is it http - POST DATA l_method TYPE string. l_method = ireq->get_header_field( '~request_method' ). IF NOT l_method = 'POST'. " EXIT. ENDIF. * Read all Header fields DATA headerfields TYPE tihttpnvp. CALL METHOD ireq->get_header_fields CHANGING fields = headerfields. * Get response content DATA lo_mr_api TYPE REF TO if_mr_api. DATA: l_folder TYPE boole_d, l_mimetype TYPE sdokfilaci-mimetype. DATA l_xstring TYPE xstring. * instantiate MIME API class CALL METHOD cl_mime_repository_api=>if_mr_api~get_api RECEIVING r_mr_api = lo_mr_api. * Get PDF file from MIME repository CALL METHOD lo_mr_api->get EXPORTING i_url = '/SAP/Zhello_world/How-to_ADS.pdf' IMPORTING e_is_folder = l_folder e_content = l_xstring e_mime_type = l_mimetype EXCEPTIONS parameter_missing = 1 error_occured = 2 not_found = 3 permission_failure = 4 OTHERS = 5. IF sy-subrc = 0. l_mime = l_mimetype. " = application/pdf ires->set_header_field( name = 'content-type' value = l_mime ). * Send result Body content ires->set_data( l_xstring ). ires->set_status( code = 200 reason = 'OK' ). ELSE. ires->set_status( code = 400 reason = 'hier stimmt was nicht' ). ENDIF.
SAP = Client
..Code: Alles auswählen.
*&---------------------------------------------------------------------* *& Report YBC_HTTP_CLIENT_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ybc_http_client_test. * data declarations DATA: client TYPE REF TO if_http_client. DATA: host TYPE string VALUE 'http://xamp:8081', errortext TYPE string. DATA: uri TYPE string, subrc TYPE i, version TYPE i, request TYPE REF TO if_http_request. DATA res_data TYPE xstring. DATA res_cdata TYPE string. DATA:buffer TYPE xstring, conv TYPE REF TO cl_abap_conv_in_ce, buf TYPE TABLE OF tab512 WITH HEADER LINE, bytes_read TYPE i, fname TYPE string VALUE 'c:\temp\000.html'. DEFINE m_err_exit. if sy-subrc <> 0. call method client->get_last_error importing code = subrc message = errortext. exit. endif. END-OF-DEFINITION. CALL METHOD cl_http_client=>create_by_url EXPORTING url = host IMPORTING client = client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. m_err_exit. client->request->set_header_field( name = '~request_method' value = 'POST' ). CALL METHOD client->request->set_form_field EXPORTING name = '$get' value = 'checked'. CALL METHOD client->send *exporting timeout = timeout EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. m_err_exit. CALL METHOD client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. m_err_exit. *CALL METHOD client->listen * RECEIVING * client = client * EXCEPTIONS * http_communication_failure = 1 * http_no_open_connection = 2 * others = 3. *IF sy-subrc <> 0. * CALL METHOD client->get_last_error * IMPORTING * code = subrc * message = errortext. * * EXIT. *ENDIF. *CALL METHOD client->response->get_data * RECEIVING * data = res_data. CALL METHOD client->response->get_cdata RECEIVING data = res_cdata. m_err_exit. CALL METHOD client->close EXCEPTIONS http_invalid_state = 1 OTHERS = 2. m_err_exit.
gruß Jens
Code: Alles auswählen.
REPORT z_webservice.
DATA: client TYPE REF TO if_http_client.
DATA: host TYPE string VALUE 'http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/^xa^cfa,50^fo100,100^fdHelloWorld^fs^xz',
errortext TYPE string.
DATA: uri TYPE string,
subrc TYPE i,
version TYPE i,
request TYPE REF TO if_http_request.
DATA: res_data TYPE xstring.
DATA: res_cdata TYPE string.
DATA: buffer TYPE xstring,
conv TYPE REF TO cl_abap_conv_in_ce,
buf TYPE TABLE OF tab512 WITH HEADER LINE,
bytes_read TYPE i,
fname TYPE string VALUE 'C:\Users\a.offerm\Desktop\LabelSAP\a.pdf'.
DEFINE m_err_exit.
if sy-subrc <> 0.
call method client->get_last_error
importing
code = subrc
message = errortext.
exit.
endif.
END-OF-DEFINITION.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = host
IMPORTING
client = client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
m_err_exit.
*client->request->set_header_field( name = '~request_method'
*value = 'POST' ).
client->request->set_header_field( name = '~request_method'
value = 'POST' ).
CALL METHOD client->request->set_form_field
EXPORTING
name = 'Accept'
value = 'application/pdf'.
CALL METHOD client->send
*exporting timeout = timeout
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
m_err_exit.
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
m_err_exit.
*CALL METHOD client->listen
* RECEIVING
* client = client
* EXCEPTIONS
* http_communication_failure = 1
* http_no_open_connection = 2
* OTHERS = 3.
*IF sy-subrc <> 0.
* CALL METHOD client->get_last_error
* IMPORTING
* code = subrc
* message = errortext.
*
* EXIT.
*ENDIF.
*CALL METHOD client->response->get_data
* RECEIVING
* data = res_data.
CALL METHOD client->response->get_cdata
RECEIVING
data = res_cdata.
m_err_exit.
CALL METHOD client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
m_err_exit.
Code: Alles auswählen.
*&---------------------------------------------------------------------*
*& Report Z_WEBSERVICE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_webservice.
DATA: client TYPE REF TO if_http_client.
DATA: host TYPE string VALUE 'http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/^xa^cfa,50^fo100,100^fdHelloWorld^fs^xz',
errortext TYPE string.
DATA: uri TYPE string,
subrc TYPE i,
version TYPE i,
request TYPE REF TO if_http_request.
DATA: res_data TYPE xstring.
DATA: res_cdata TYPE string.
DATA: buffer TYPE xstring,
conv TYPE REF TO cl_abap_conv_in_ce,
buf TYPE TABLE OF tab512 WITH HEADER LINE,
bytes_read TYPE i,
"fname TYPE string VALUE 'C:\Users\a.offerm\Desktop\LabelSAP\a.pdf'.
fname TYPE string VALUE 'C:\temp\data.png'.
DEFINE m_err_exit.
if sy-subrc <> 0.
call method client->get_last_error
importing
code = subrc
message = errortext.
exit.
endif.
END-OF-DEFINITION.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = host
IMPORTING
client = client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
m_err_exit.
client->request->set_header_field( name = '~request_method'
value = 'GET' ).
CALL METHOD client->send
*exporting timeout = timeout
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
m_err_exit.
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
m_err_exit.
DATA: i TYPE i,
lt_bin_tab TYPE solix_tab.
CALL METHOD client->response->get_data
RECEIVING
data = res_data.
m_err_exit.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = res_data
* APPEND_TO_TABLE = ' '
IMPORTING
OUTPUT_LENGTH = i
TABLES
binary_tab = lt_bin_tab
.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = i
filename = fname
filetype = 'BIN'
CHANGING
data_tab = lt_bin_tab
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.
IF sy-subrc <> 0.
ENDIF.
CALL METHOD client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
m_err_exit.