The following ABAP program executes an HTTP request. For testing purposes, the call destination is the sending system itself, so that the Application Server ABAP is used in both the client role and the server role:
Caution
This program shows a simplified sample code. It is not meant as a template for real coding.
Syntax
End of the code. |
The following parameters must be entered:
Note
If nothing is entered, the following 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 in Implementation (default: /sap/public/ping).
Note
If the connection using the Destination (SM59) is used when the client object is created, these parameters must be set accordingly in the destination.
Syntax
End of the code. |
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 (method create_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.
Note
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) |
|
Syntax
End of the code. |
Syntax
End of the code. |
Syntax
End of the code. |
Header fields are then set for the requests. Setting the HTTP request method to GET is optional.
Note
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.
Syntax
End of the code. |
Any number of fields in the control block can then be filled.
Additional Options
You can deactivate the attributes PROPERTYTYPE_LOGON_POPUP, PROPERTYTYPE_REDIRECT and PROPERTYTYPE_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 values CO_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.
The request is now sent.
Syntax
End of the code. |
Here, the last error must be queried again, using client->get_last_error.
The response is now received and the client object is filled with the response data.
Syntax
End of the code. |
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:
Syntax
End of the code. |
To document the successful test, the output routine is called at this point.
Syntax
End of the code. |