Fm to Consume Web Services Manually


Import Parameters:

 Imports

Tables:

 tables

Source Code:

FUNCTION ZCONSUME_WS_MANUALLY.

*”———————————————————————-

*”*”Local Interface:

*”  IMPORTING

*”     REFERENCE(I_URL) TYPE  STRING

*”     REFERENCE(I_USERNAME) TYPE  STRING OPTIONAL

*”     REFERENCE(I_PASSWORD) TYPE  STRING OPTIONAL

*”  TABLES

*”      IT_XML TYPE  STRING_TABLE OPTIONAL

*”      RT_XML TYPE  STRING_TABLE OPTIONAL

*”———————————————————————-

data lo_client         type ref to if_http_client.

data ls_smum_xmltb     type smum_xmltb           .

data gv_ws_string      type string               .

data lv_string         type string               .

data lv_length         type i                    .

data gv_ws_stringx     type xstring              .

data lv_content_leng   type string               .

data rt_smum_xmltb     type table of smum_xmltb  .

data lv_response_data  type string               .

data lv_response_datax type xstring              .

data lt_return         type table of bapiret2    .

* 1. Collecting XML in string.

loop at it_xml into lv_string.

 concatenate gv_ws_string lv_string

        into gv_ws_string.

 clear lv_string.

endloop.

* 2. Getting lenght of xml

lv_length lv_content_leng

          strlengv_ws_string ).

* 3. Creating client ocject

cl_http_client=>create_by_url(

      exporting url i_url

      importing client lo_client ).

check lo_client is bound.

* 4. Setting uname and pass if filled

if i_username is not initial and

   i_password is not initial.

   lo_client->authenticateusername i_username

                            password i_password ).

endif.

lo_client->propertytype_accept_cookie

         if_http_client=>co_enabled.

lo_client->request->set_header_field(

   name  if_http_form_fields_sap=>sap_client value

         ‘100’ ).

lo_client->propertytype_logon_popup

         lo_client->co_disabled.

lo_client->request->set_header_field(

    name ‘~request_method’

   value ‘POST’ ).

lo_client->request->set_header_field(

    name ‘~server_protocol’

   value ‘HTTP/1.1’ ).

lo_client->request->set_header_field(

    name ‘Content-Type’

   value ‘text/xml; charset=utf-8’ ).

lo_client->request->set_header_field(

    name ‘Content-Length’

   value lv_content_leng ).

lo_client->request->set_cdata(

    data gv_ws_string

  length lv_length ).

* 5. Sending xml package

lo_client->send( ).

* 6. Receiving xml package

lo_client->receive( ).

lv_response_data lo_client->response->get_cdata( ).

lo_client->close( ).

call function ‘SCMS_STRING_TO_XSTRING’

 exporting

   text   lv_response_data

 importing

   buffer lv_response_datax.

call function ‘SMUM_XML_PARSE’

 exporting

   xml_input lv_response_datax

 tables

   xml_table rt_smum_xmltb

   return    lt_return.

* Writing xml return to IT_RETURN

loop at rt_smum_xmltb into ls_smum_xmltb.

  clear lv_string.

  lv_string ls_smum_xmltbcvalue.

  append lv_string to rt_xml.

endloop.

ENDFUNCTION.

Leave a comment

Your email address will not be published. Required fields are marked *