sábado, abril 16, 2011

reCAPTCHA & ABAP #verification process#




If we need to verify a reCAPTCHA by a SAP server we can use the following way to do it.

Good luck!

We should create a “HTTP Connection to External Server” by SM59 tcode.
It will look like:



The verification process would be:


DATA: http_dest TYPE rfcdest VALUE 'ZTEST_HTTP'.

DATAclient TYPE REF TO if_http_client.
DATA: request TYPE REF TO if_http_request,
      rc TYPE sy-subrc,
      http_rc TYPE sy-subrc,
      xml_xstring TYPE xstring.

DATA: convin        TYPE REF TO cl_abap_conv_in_ce,
      l_data TYPE string.

START-OF-SELECTION.

  cl_http_client=>create_by_destination( EXPORTING
  destination = http_dest IMPORTING client = client ).

  CALL METHOD client->request->set_method(
    if_http_request=>co_request_method_post ).

*optional Request fields
  CALL METHOD client->request->set_form_field
    EXPORTING
      name  = 'privatekey'
      value = 'your_privateKey'.

  CALL METHOD client->request->set_form_field
    EXPORTING
      name  = 'remoteip'
      value = 'client_remote_ip'.

  CALL METHOD client->request->set_form_field
    EXPORTING
      name  = 'challenge'
      value = 'the_challenge_id'.

  CALL METHOD client->request->set_form_field
    EXPORTING
      name  = 'response'
      value = 'user_response'.


* send and receive
  CALL METHOD client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      http_invalid_timeout       = 4
      OTHERS                     = 5.
  IF sy-subrc <> 0.
    RAISE connection_error.
  ENDIF.

  CALL METHOD client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.

  rc = sy-subrc. "error receive

  IF rc = 0.
**http status code
    client->response->get_status( IMPORTING code = http_rc ).

    IF http_rc <> 200.
      """KO
    ELSE"status 200 ->>OK
      CLEAR: xml_xstring.
      xml_xstring = client->response->get_data( ).
* show the
      CALL METHOD cl_abap_conv_in_ce=>create
        EXPORTING
          encoding    = 'UTF-8'
          endian      = 'L'
          ignore_cerr = 'X'
          replacement = '#'
          input       = xml_xstring             " Xstring
        RECEIVING
          conv        = convin.

      CALL METHOD convin->read
        IMPORTING
          data = l_data.                  " String

      WRITE l_data.
    ENDIF.
  ENDIF.

* VERY IMPORTANT: close your connection
  client->close( ).


The possible results are going to be:

false#incorrect-captcha-sol
or
true

:)

No hay comentarios: