2 Replies Latest reply on Jun 30, 2015 3:36 PM by Djaydine

    UTL_HTTP.get_cookie_count always return 0

    Djaydine

      Hello,

       

      I have an issue with utl_http which seems to not work as the documentation says. I think there is something I am doing wrong but after some researches, I can't find what I do in the bad way.

       

      I would like to be able to read the cookie I receive in the response to my GET request. According to the Oracle Documentation of UTL_HTTP, I thought that a simple call to UTL_HTTP.GET_COOKIES would do what I want. But UTL_HTTP.GET_COOKIES, which return a table of cookies, is always empty whereas, in the header I receive in response to my request, there is effectiuvely a cookie (header Set-Cookie).

       

      This is my PL/SQL code :

       

      --API configuration
      UTL_HTTP.set_cookie_support( TRUE ); 
      UTL_HTTP.clear_cookies();
      UTL_HTTP.set_response_error_check(TRUE);
      
      --Start request
      m_Request := UTL_HTTP.begin_request(
                                  url => 'http://www2.leboncoin.fr/ai/form/3?ca=22_s',
                                  method => 'GET',
                                  http_version => 'HTTP/1.1'
                              );
      
      --Headers                     
      UTL_HTTP.set_header(m_Request, 'User-Agent', 'Chrome/43.0.2357.130');
              
      --Execute the request
      m_Response := UTL_HTTP.GET_RESPONSE(m_Request);
      
      --Loop throught the headers
      FOR i IN 1..UTL_HTTP.GET_HEADER_COUNT(m_Response)
      LOOP
          UTL_HTTP.GET_HEADER(m_Response, i, v_Name, v_Value);
                  
          --Display cookies of the response
          IF (UPPER(v_Name) = 'SET-COOKIE')
          THEN
              DBMS_OUTPUT.PUT_LINE(v_Name || ': ' || v_Value);
           END IF;
       END LOOP;
      
      --Nb of cookies
      UTL_HTTP.GET_COOKIES(v_Cookies);
      dbms_output.put_line('Nb of cookies : ' || v_Cookies.COUNT);
      
      
      --Close response
      
      UTL_HTTP.end_response(m_Response);
      

      The result of the execution of this code is :

      Set-Cookie: cookieFrame=1; expires=Mon, 27-Jun-2016 22:06:11 GMT; path=/; domain=.leboncoin.fr

      Set-Cookie: s=red1xcbcd9d034c6855c0b67f96f396bf3d823922534c; path=/; domain=.leboncoin.fr

      Nb of cookies : 0

      Please, could you help me to find why ULT_HTTP says that there is no cookies in the result wheareas the ares 2 cookies in my example ?

       

      Thank you very much for your help.

        • 1. Re: UTL_HTTP.get_cookie_count always return 0
          MNitu

           

          Hi

          I’m not a specialist but I think that I understood what you have to do:

           

          First you send a HTTP request to a server with no cookies, then server responds embedding maybe some cookies via Set-Cookie) Now you have to extract these cookies along with theirs properties and store them persistently in order to reuse them. Next time when you make your request you will send theses stored cookies and examine if the server ha embedded some more cookies. If this is the case you have to decode and store them again if they do expire. (see https://fr.wikipedia.org/wiki/Cookie_(informatique))

          declare
            m_Request       UTL_HTTP.req;
            m_Response      UTL_HTTP.resp;
            v_name          VARCHAR2(256);
            v_value         VARCHAR2(1024);
            v_Cookies       UTL_HTTP.cookie_table;
          begin
          --API configuration
            UTL_HTTP.set_cookie_support( TRUE );
            UTL_HTTP.clear_cookies();
            UTL_HTTP.set_response_error_check(FALSE);
            -- Restore previous stored cookies
            -- cookieFrame=1; expires=Wed, 29-Jun-2016 09:37:39 GMT; path=/; domain=.leboncoin.fr
            v_Cookies(1).name    := 'cookieFrame';
            v_Cookies(1).value   := '1';
            v_Cookies(1).domain  := '.leboncoin.fr';
            v_Cookies(1).expire  := systimestamp + 1; -- seems that expired cookies are silently discarded
            v_Cookies(1).path    := '/';
            v_Cookies(1).secure  := Null;
            v_Cookies(1).version := Null; 
            -- s=red1x796a32075f1b8c5314e537c60aedeb4c06e8b095; path=/; domain=.leboncoin.fr
            v_Cookies(2).name    := 's';
            v_Cookies(2).value   := 'red1x796a32075f1b8c5314e537c60aedeb4c06e8b095';
            v_Cookies(2).domain  := '.leboncoin.fr';
            v_Cookies(2).expire  := Null;             -- suppose that il never expires
            v_Cookies(2).path    := '/';
            v_Cookies(2).secure  := Null;
            v_Cookies(2).version := Null; 
            dbms_output.put_line(v_Cookies.count());
            UTL_HTTP.CLEAR_COOKIES;
            UTL_HTTP.add_cookies(v_Cookies/*, request_context*/); 
            dbms_output.put_line(utl_http.get_cookie_count());
            --Start request
            m_Request := UTL_HTTP.begin_request (
                                         url => 'http://www2.leboncoin.fr/ai/form/3?ca=22_s',
                                         method => 'GET',
                                         http_version => 'HTTP/1.1'
                                     );

             --Headers
             UTL_HTTP.set_header(m_Request, 'User-Agent', 'Chrome/43.0.2357.130');

             --Execute the request
             m_Response := UTL_HTTP.GET_RESPONSE(m_Request);

             --Loop throught the headers
             FOR i IN 1..UTL_HTTP.GET_HEADER_COUNT(m_Response)
             LOOP
                 UTL_HTTP.GET_HEADER(m_Response, i, v_Name, v_Value);

                 --Display cookies of the response
                 IF (UPPER(v_Name) = 'SET-COOKIE')
                 THEN
                     DBMS_OUTPUT.PUT_LINE(v_Name || ': ' || v_Value);
                     -- Extract cookies with theirs properties here and store them persitently
                  END IF;
              END LOOP;

             --Nb of cookies
             UTL_HTTP.GET_COOKIES(v_Cookies);
             dbms_output.put_line('Nb of cookies : ' || v_Cookies.count);

             --Close response
             UTL_HTTP.end_response(m_Response);
          end;

          • 2. Re: UTL_HTTP.get_cookie_count always return 0
            Djaydine

            Thank you MNitu for your answer.

             

            I did exactly what you said and it works perfectly!!

            I thought that UTL_HTTP automatically populates the cookies table but it seems not... obviously I was wrong !!!

             

            Thank you again!!!