Sei sulla pagina 1di 6

Example Program: Executing an

HTTP Request
The ABAP code shown here is simplified sample code. It is not meant as a
template for real coding.
The following ABAP program executes an HTTP request. For testing purposes, the call
destination is the sending system itself, so that the ABAP Application Server is used in both
the client role and the server role:

report HTTP_Test.
* data declarations
data: client type ref to if_http_client.

The following parameters must be entered:


If no data is entered, default values are used: host (default value: current host), service
(default value; port 80 for HTTP and port 443 for HTTPS), protocol (default: HTTP/1.0), path
to the required service, as described inImplementation (default: /sap/public/ping).
If the connection using the Destination (SM59) is used when the client object is created,
these parameters must be set accordingly in the destination.

data: host type string value = host.wdf.sap-ag.de,


service type string value = 8080,
path type string value = '/sap/public/ping',
errortext type string. "used for error handling
Step 1
The object of type CL_HTTP_CLIENT is now created. There are two possible procedures for
doing this:
In one case, the create method is called. Here, you need to enter the required host and port,
and you can also enter the proxy setting. This overrides any configuration that was made in
Transaction SICF (see also Configuring Proxies).
In the other case, the destination that was maintained in Transaction SM59 is used
(methodcreate_by_destination). Only the name of the destination and the client need
be specified here. In both cases, you have to define exceptions (not shown here) and
query the last error code.
SAP recommends that you use a fixed destination, as this reduces the need
for configuration of communication parameters.

The object Client can be used to access all data (request, response, header field, and so on).
The scheme determines whether HTTP or HTTPS is to be used.

* create HTTP client object(s)


call method
cl_http_client=>create
exporting host = host_str
service = service_str
proxy_host = proxy_host
proxy_service =
proxy_service
scheme = scheme
importing client = client
exceptions
argument_not_found = 1
internal_error = 2
plugin_not_active = 3
others = 4.

call method
cl_http_client=>create_by_destination
exporting destination = dest
importing client = client
exceptions
destination_not_found = 1
internal_error = 2
argument_not_found = 3
destination_no_authority = 4
plugin_not_active = 5
others = 6.

if sy-subrc <>0.
write: / Create failed, subrc = ', sy-subrc.
exit.
endif.
Header fields are then set for the requests. Setting the HTTP request method to GET is
optional.
If this field is not set, the system uses GET if the HTTP request does not
contain a body. Otherwise, it uses POST.
You must set the request URI unless the whole path was specified as a path prefix using
the Destination (SM59). Here you use method set_request_uri of class cl_http_utility.

* set http method GET


call method client->request->set_method(
if_http_request=>co_request_method_get ).
* set protocol version
if protocol = 'HTTP/1.0'.
client->request->set_version(
if_http_request=>co_protocol_version_1_0 ).
else.
client->request->set_version(
if_http_request=>co_protocol_version_1_1 ).
endif.
* set request uri (/<path>[?<querystring>])
cl_http_utility=>set_request_uri( request = client->request
uri = uri ).

Step 2
Any number of fields in the control block can then be filled.
Further Options
You can deactivate the
attributes PROPERTYTYPE_LOGON_POPUP, PROPERTYTYPE_REDIRECT andPROPERTYTYPE
_ACCEPT_COMPRESS that have the default value CO_ENABLED (set to CO_DISABLED).
You can set PROPERTYTYPE_ACCEPT_COOKIE to different values. The default value
is CO_DISABLED, but valuesCO_ENABLED (always accept cookies), CO_PROMPT (send a
dialog box when cookies are received to ask if the cookie should be accepted)
and CO_EVENT (cookie handling using event EVENTKIND_HANDLE_COOKIE of
interface IF_HTTP_CLIENT) are also available.
You can set PROPERTYTYPE_ACCEPT_COMPRESS to the value CO_DISABLED so that the
partner can send its data in non-compressed form.
Before the request is sent, you can also enter the necessary logon data using
the AUTHENTICATE()method of interface IF_HTTP_CLIENT.

Step 3
The request is now sent.

* Send
call method client->send
exporting timeout = timeout
exceptions http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
others = 4.
if sy-subrc <> 0.
call method client->get_last_error
importing code = subrc
message = errortext.
write: / 'communication_error( send )',
/ 'code: ', subrc, 'message: ', dummy.
exit.
endif.
Here, the last error must be queried again, using client -> get_last_error.

Step 4
The response is now received and the client object is filled with the response data.

* receive
call method client->receive
exceptions http_communication_failure = 1
http_invalid_state = 2

http_processing_failed = 3
others = 4.
if sy-subrc <> 0.
call method client->get_last_error
importing code = subrc
message = errortext.
write: / 'communication_error( receive )',
/ 'code: ', subrc, 'message: ', dummy.
exit.
endif.
Exceptions and error queries must be inserted here too.
If the connection is no longer being used, you must close the client object and the
connection as follows:

* close
call method client->close
exceptions http_invalid_state = 1
others = 2.
To document the successful test, the output routine is called at this point.

* output
perform write_document.

Parallel Requests
The ABAP code shown here is simplified sample code. It is not meant as a
template for real coding.

Use
You can send requests to various servers in parallel and to wait for all the responses
simultaneously. The methodLISTEN is then used to find out which request is currently being
responded to.
This procedure is useful if several requests have been assigned to different servers with
widely differing response times.

Activities
You can also add to the program by inserting the following lines. In additional parameters,
specify that you want to use the LISTEN method and the number of requests that you want
to send.

parameters: listen type c default space,


times type i default '1',

Now the system waits for the responses to the requests. The responses are received in the
order in which the server sends them.

*Empfangen mit listen (Parallelisierung von #times Requests)


do times times.
if not listen is initial.
call method client->listen
receiving client = cclient
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 = dummy.
write: / listen,
/ 'code: ', subrc, 'message: ', dummy.
exit.
endif.

The incoming responses are re-assigned to the requests.

* Zuordnung der Responses zu den Requests


* wenn es mehr Requests gibt, alle Antworten aufnehmen
client = cclient.
call method client->receive
exceptions http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
others = 4.
if sy-subrc <> 0.
call method client->get_last_error
importing code = subrc
message = dummy.
write: / 'communication_error( receive )',
/ 'code: ', subrc, 'message: ', dummy.
exit.
endif.
...
enddo.

You can then specify how often a request should be dropped (parameter times). You can see
in the output the order in which the responses were received. This does not have to be the
order in which the requests were sent.

Potrebbero piacerti anche