Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Open URL in browser by sending http post data

Former Member
0 Likes
6,672

Hi experts,

Is there any method to open a web page from abap code while sending http post data?

I want something like a mixture of abilities below methods :

CL_HTTP_CLIENT

cl_gui_html_viewer

I want to open an url like cl_gui_html_viewer and i want to send http post data alike CL_HTTP_CLIENT class while web page is opening on my gui..

Thanks..

Hi experts,

Is there any method to open a web page from abap code while sending http post data?

I want something like a mixture of abilities below methods :

CL_HTTP_CLIENT

cl_gui_html_viewer

I want to open an url like cl_gui_html_viewer and i want to send http post data alike CL_HTTP_CLIENT class while web page is opening on my gui..

Thanks..

18 REPLIES 18
Read only

Kiran_Valluru
Active Contributor
0 Likes
3,834

Hi.,

Check SAPHTML_EVENTS_DEMO Standard Program. This will help u how to send data using GET and POST Methods.,

hope this helps u.,

Thanks & Regards,

Kiran

Read only

0 Likes
3,834

Thank you for anwser Kiran, but in CL_GUI_HTML_VIEWER class, i didnt see any method which can set post data before calling url (by show_url method). And also M_POSTDATA attiribute of the class is private and it doesnt let you change..

Regards

Ekrem

Read only

0 Likes
3,834

Hi again,

It seems, this issue is not possible? If it is, i'm going to close this thread as an unanswered question..

Regards..

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,834

Hi Arete Customers,

Is there any method to open a web page from abap code while sending http post data?

This question sounds strange! Why do you want to do 2 different things as the same time (I would answer "do parallel programming"), what do you want to do exactly?

Sandra

Read only

0 Likes
3,834

Hi Sandra,

Let me explain:

I want to open a web page in gui (e.g. www.mycityweather.com) and i want to send some data to this page using http post method (e.g. city name..). But I cant send any data using querystring method because of security issues. Web side developer will catch this data.

Regards

Ekrem

Read only

0 Likes
3,834

Hi Ekrem,

well I think that's only one question. In CL_HTTP_REQUEST, you have IF_HTTP_REQUEST~SET_METHOD. It is POST by default. You have an example here: [SAP Library: Example Program: Executing an HTTP Request|http://help.sap.com/saphelp_nw70/helpdata/en/1f/93163f9959a808e10000000a114084/frameset.htm]

Doesn't it work? What error message do you get?

Sandra

Read only

0 Likes
3,834

Or do you mean you want to do a kind of screen input simulation by scripting? (like ECATT, batch input, or SAPGUI scripting...)

Read only

0 Likes
3,834

Hi Sandra,

Thanks for your answer. The thing that i want is simple. I will call a web page in sap gui, and i will enter some input fields in this page, that's all. I should see the web page in my gui, the most important thing is this actually.

Regards.

Ekrem

Read only

0 Likes
3,834

So, you just have to do the program from the template given in the link above, using CL_HTTP_REQUEST.

If you want then to capture the result and display it, you'll take it from the response, and display it using CL_GUI_HTML_VIEWER

Regards,

Sandra

Read only

0 Likes
3,834

Hi Sandra,

This method didnt come to me feasible. Or maybe i didnt understand you well. Because once i will call the web page and i will continue to work there, because it is much more like web-portal (not like sap or microsoft portal), carrying every request in an another object (CL_HTTP_REQUEST), i dont know, there should be another way. Anyway as i told maybe i couldnt understand your way clearly..

Ekrem

Read only

0 Likes
3,834

Hi,

I think that's a shared misunderstanding

HTTP is the protocol used to exchange data between internet and the web browser (IE, firefox, safari, etc.) It is used to carry HTML, images, etc.

CL_HTTP_CLIENT (sorry I first said CL_HTTP_REQUEST, but that's not the initial class to invoke) is used to send an HTTP request from ABAP to the internet, and get the response data, without using the web browser (in fact it's like SAP is the web browser).

Then, as it seems you want to display the result returned by the web page, then you have to call CL_GUI_HTMLVIEWER and feed it from the response data.

Did you try, at least?

Best regards,

Sandra

Read only

0 Likes
3,834

Hi Ekrem ,

Sandra is right. All you need is passing response to cl_gui_html_viewer. You can simply use cl_gui_html_viewer to display any custom html content. So after you have your response back from HTTP request with related parameters , just bind it to cl_gui_html_viewer.

http://wiki.sdn.sap.com/wiki/display/Snippets/ABAP-DisplayingCustomHTMLContentUsing+cl_gui_html_viewer

Read only

0 Likes
3,834

Hi Sandra and Huseyin,

Thanks for your answers. I wrote some code according to your answers. But i have some limitations from server side. SAP Server doesn't have internet connection. And as i know CL_HTTP_CLIENT is working at server side but cl_gui_html_viewer is working client side. Because of this limitation i couldnt connect and store http response data in cl_http_client object. I think, my situtation is a little bit difficult and interesting. I looked in CL_HTTP_CLIENT class but i didnt see any parameter for client side working.

I wrote below code, but as i said, sap server doesnt have direct internet connection..

DATA  html_control TYPE REF TO cl_gui_html_viewer.
DATA: l_empty_co    TYPE REF TO cl_gui_container.
data : gv_url type string VALUE 'http://....'.
data : url_char type c LENGTH 255.
DATA : HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT.
data : post_data_string TYPE string.
data : data_length TYPE i.
DATA : lt_table type TABLE OF char255 WITH HEADER LINE.
DATA : page_content type xstring.
data : LT_EX_TAB  LIKE  LXE_XTAB OCCURS 1 WITH HEADER LINE.


CREATE OBJECT html_control
  EXPORTING
    parent = l_empty_co.


call method cl_http_client=>create_by_url
  exporting
    url    = gV_URL
  importing
    client = http_client
  exceptions
    others = 1.

call method http_client->request->set_header_field
  EXPORTING
    name  = '~request_method'
    value = 'POST'.



CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = 'Content-Type'
    value = 'application/x-www-form-urlencoded'.


post_data_string = 'lang=TR....'.

data_length = strlen( post_data_string ).

CALL METHOD http_client->request->set_cdata
  EXPORTING
    data   = post_data_string
    offset = 0
    length = data_length.

  call method http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.

 IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE 'I' NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 DISPLAY LIKE SY-MSGTY.
  exit.
 ENDIF.

*   5 - Get the result back

  call method http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

 IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE 'I' NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 DISPLAY LIKE SY-MSGTY.
  exit.
 ENDIF.

*   6 - Get the content of the returned page
  page_content = http_client->response->get_data( ).

  CALL FUNCTION 'LXE_COMMON_XSTRING_TO_TABLE'
    EXPORTING
      IN_XSTRING        = page_content
    TABLES
      EX_STRING         = LT_EX_TAB
            .

  CLEAR lt_table[].
  LOOP AT LT_EX_TAB.
    APPEND lt_ex_tab-text to lt_table.
  ENDLOOP.

  url_char = gv_url.

 CALL METHOD  html_control->LOAD_DATA
    EXPORTING
      URL                  = url_char
   IMPORTING
      ASSIGNED_URL         = url_char
   CHANGING
      DATA_TABLE           = lt_table[]
   EXCEPTIONS
     DP_INVALID_PARAMETER = 1
     DP_ERROR_GENERAL     = 2
     CNTL_ERROR           = 3
     others               = 4
         .
 IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE 'I' NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 DISPLAY LIKE SY-MSGTY.
  exit.
 ENDIF.

  CALL METHOD html_control->('DETACH_URL_IN_BROWSER')
    EXPORTING
      url = gv_url.
  cl_gui_cfw=>flush( ).

Read only

0 Likes
3,834

If Sap Server doesn't have access to the internet , it is a problem.

May be you could use javascript. Like abap , use http post methods to get response.

http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}

http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit

Read only

0 Likes
3,834

Thanks Huseyin. I'm going to do a temporary web page using load_data for cl_gui_html_viewer class. On this page i will use some javascript code like u sent. I think this code will solve my problem. Thanks again

Regards

Ekram

Read only

0 Likes
3,834

Glad that you have figured out the issue. Thanks.

Regards.

Read only

0 Likes
3,834

@Hüseyin: very good proposition! It works very well, I have done/tested the code below as demo.

@Ekram: the javascript works, but maybe your admins could think to open a web connection, as maybe it is planned to use and develop web services...


REPORT.
* This program simulates an action on a web page, by using the frontend browser.
* Another solution (to be preferred usually) is to use SAP's web connection, using CL_HTTP_CLIENT
* <FORM ACTION="http://www.website.com/action.php" METHOD="POST">
*   <INPUT name="FIRSTNAME"></INPUT>
*   <INPUT name="LASTNAME"></INPUT>
*   <BUTTON type="SUBMIT">EXECUTE</BUTTON>
* </FORM>
PARAMETERS action TYPE text132 DEFAULT 'http://www.website.com/action.php'.
PARAMETERS method TYPE char10 DEFAULT 'POST'.
PARAMETERS param1n TYPE text132 DEFAULT 'FIRSTNAME'.
PARAMETERS param1v TYPE text132 DEFAULT 'John'.
PARAMETERS param2n TYPE text132 DEFAULT 'LASTNAME'.
PARAMETERS param2v TYPE text132 DEFAULT 'Doe'.
DATA html_viewer TYPE REF TO cl_gui_html_viewer.

SELECTION-SCREEN BEGIN OF SCREEN 1001.
PARAMETERS dummy.
SELECTION-SCREEN END OF SCREEN 1001.

AT SELECTION-SCREEN OUTPUT.
  IF sy-dynnr = 1001 AND html_viewer IS INITIAL.
    DATA l_params TYPE string.
    TYPES : BEGIN OF ty_ls_form_field,
              name TYPE string,
              value TYPE string,
            END OF ty_ls_form_field.
    DATA lt_form_field TYPE TABLE OF ty_ls_form_field.
    FIELD-SYMBOLS <ls_form_field> TYPE ty_ls_form_field.
    DEFINE mac_add_form_field.
      append initial line to &1 assigning <ls_form_field>.
      <ls_form_field>-name = &2.
      <ls_form_field>-value = &3.
    END-OF-DEFINITION.
    mac_add_form_field lt_form_field: param1n param1v, param2n param2v.
    DATA l_sep TYPE string.
    LOOP AT lt_form_field ASSIGNING <ls_form_field>.
      REPLACE ALL OCCURRENCES OF '"' IN <ls_form_field>-value WITH '"'.
      CONCATENATE l_params l_sep <ls_form_field>-name ':"' <ls_form_field>-value '"' INTO l_params.
      l_sep = ','.
    ENDLOOP.
    CREATE OBJECT html_viewer
      EXPORTING
        parent             = cl_gui_container=>screen0
      EXCEPTIONS
        cntl_error         = 1
        cntl_install_error = 2
        dp_install_error   = 3
        dp_error           = 4.
    DATA assigned_url(255).
    PERFORM load_html_data USING html_viewer action l_params method
          CHANGING assigned_url.
    CALL METHOD html_viewer->show_data
      EXPORTING
        url        = assigned_url
      EXCEPTIONS
        cntl_error = 1.
  ENDIF.

AT SELECTION-SCREEN.
  IF sy-dynnr = 1000 AND sy-ucomm = 'ONLI'.
    CALL SELECTION-SCREEN 1001.
  ENDIF.

AT SELECTION-SCREEN ON EXIT-COMMAND.
  IF sy-dynnr = 1001 AND html_viewer IS BOUND.
    html_viewer->free( ).
    FREE html_viewer.
  ENDIF.
*---------------------------
FORM load_html_data USING html_viewer TYPE REF TO cl_gui_html_viewer
       i_url i_params i_method
      CHANGING assigned_url.
  DATA l_string TYPE string.
  DATA lt_data TYPE TABLE OF text132.
  CONCATENATE
  `<html>`
  `<head>`
  `<scrip`
&`t language="javacrip`
&`t"><!--`
  `function autostart() {`
  `post_to_url("&URL", {&PARAMS}, "&METHOD");`
  `}`
  `function post_to_url(path, params, method) {`
  `    // The rest of this code assumes you are not using a library.`
  `    // It can be made less wordy if you use one.`
  `    var form = document.createElement("form");`
  `    form.setAttribute("method", method);`
  `    form.setAttribute("action", path);`
  `    for(var key in params) {`
  `        var hiddenField = document.createElement("input");`
  `        hiddenField.setAttribute("type", "hidden");`
  `        hiddenField.setAttribute("name", key);`
  `        hiddenField.setAttribute("value", params[key]);`
  `        form.appendChild(hiddenField);`
  `    }`
  `    document.body.appendChild(form);`
  `    form.submit();`
  `}`
  `// -->`
  `</script>`
  `</head>`
  `<body onloa`
&`d="autostart()"></body>`
  `</html>` INTO l_string SEPARATED BY cl_abap_char_utilities=>cr_lf.
  REPLACE '&URL' IN l_string WITH i_url.
  REPLACE '&PARAMS' IN l_string WITH i_params.
  REPLACE '&METHOD' IN l_string WITH i_method.
  CALL FUNCTION 'SWA_STRING_TO_TABLE'
    EXPORTING
      character_string           = l_string
    IMPORTING
      character_table            = lt_data
    EXCEPTIONS
      no_flat_charlike_structure = 1
      OTHERS                     = 2.
  CALL METHOD html_viewer->load_data
    IMPORTING
      assigned_url         = assigned_url
    CHANGING
      data_table           = lt_data
    EXCEPTIONS
      dp_invalid_parameter = 1
      dp_error_general     = 2
      cntl_error           = 3
      OTHERS               = 4.
ENDFORM.                    "load_html_data

Sandra

Read only

0 Likes
3,834

Thanks Sandra