Shaban Mousa
Shaban Mousa

Reputation: 154

PL/SQL code to get redirected URL

I've been asked to write PL/SQL code to get the URL of the directed page.

One URL may be automatically redirected to other URL.

For example the link http://www.arabseed.com maybe redirected to http://www.arabseed.com/ar/home.html.

So my PL/SQL block should accept first URL and return second one. I have looked @ htp pkg in Oracle API and it did not help me a lot.

Upvotes: 2

Views: 5957

Answers (2)

Daniel Haviv
Daniel Haviv

Reputation: 1036

I'm pretty sure your boss didn't mean for other people to do your work for you.
If you'd look at the UTL_HTTP documentation you will find examples that do 70% of the work for you.

In any case, here what you are looking for (for just one redirect, I trust you'll be able to modify it to a series of redirects):

SET SERVEROUTPUT ON SIZE 40000

DECLARE
  req   UTL_HTTP.REQ;
  resp  UTL_HTTP.RESP;
  name  VARCHAR2(256);
  value VARCHAR2(1024);
BEGIN
  req := UTL_HTTP.BEGIN_REQUEST('http://www.arabseed.com');
  UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
  UTL_HTTP.SET_FOLLOW_REDIRECT(req, 0);
  resp := UTL_HTTP.GET_RESPONSE(req);
  IF(resp.STATUS_CODE in (UTL_HTTP.HTTP_MOVED_PERMANENTLY,UTL_HTTP.HTTP_TEMPORARY_REDIRECT, UTL_HTTP.HTTP_FOUND))
  THEN
    FOR i IN 1..UTL_HTTP.GET_HEADER_COUNT(resp) LOOP
       UTL_HTTP.GET_HEADER(resp, i, name, value);
       if(name ='Location')
       THEN
             DBMS_OUTPUT.PUT_LINE('Redirecting to:' || value);
       END IF;
     END LOOP;
   END IF;  
  UTL_HTTP.END_RESPONSE(resp);
END;

Upvotes: 5

HAL 9000
HAL 9000

Reputation: 3985

from the info you gave us I'm only able to come up with this:

function get_redirected_url (url varchar2) return varchar2 is
begin
  case url
    when 'http://www.arabseed.com' then return 'http://www.arabseed.com/ar/home.html';
    when /* other URLs go here */
    else return '';
  end;
end;

However, I'm pretty sure that's not what you've been asked for. You'll have to refine your question.

Perhaps it's all your managers fault, because in fact the webserver would handle the default redirects.

Upvotes: -2

Related Questions