Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, V Siva.

Asked: June 07, 2000 - 3:10 pm UTC

Last updated: January 08, 2014 - 6:35 pm UTC

Version: 7.3.2.3.0

Viewed 100K+ times! This question is

You Asked

Hi Tom,

I want to know the usage of UTL_HTTP package. Can u explain it with an example?


Thanks
Siva

and Tom said...

Well, UTL_HTTP was introduced in 7.3.3 and I see you have 7.3.2 so you might not have access to it. Here is how to use utl_http. It has 2 interfaces

o one simply returns a URL requested page as a single string. Works for small pages upto 2000 bytes.

o the other returns a PLSQL table (like an array) of 2000 byte strings that represent the entire page.

In Oracle8i, release 8.1, support for using a proxy server was introduced as well. Here are 2 examples showing the simple and then the table based interfaces:

ops$tkyte@8i> create or replace
2 procedure p( p_string in varchar2 )
3 is
4 l_string long default p_string;
5 begin
6 loop
7 exit when l_string is null;
8 dbms_output.put_line( substr( l_string, 1, 250 ) );
9 l_string := substr( l_string, 251 );
10 end loop;
11 end;
12 /

Procedure created.

P is a little procedure to help us print out the text we get for this demo..

ops$tkyte@8i>
ops$tkyte@8i> declare
2 l_page long;
3 l_url varchar2(35) default
4 '</code> http://aria.us.oracle.com/';
  5  begin
  6      l_page := utl_http.request( l_url );
  7  
  8      p( l_page );
  9  end;
 10  /
<HTML>
<HEAD>
<TITLE>Oracle Service Industries</TITLE>
</HEAD>
<FRAMESET COLS="130,*"
border=0>
<FRAME SRC="nav.html" NAME="sidebar" frameborder=0>
<FRAME
SRC="
http://aria.us.oracle.com/sb/web$sb.folder.home"
NAME="body" frameborder="0" marginheight=
"0"
marginwidth="0">
</FRAMESET>
</BODY>
</HTML>
PL/SQL procedure successfully completed.

<b>That was the simplest way to call UTL_HTTP - just send it a URL and get back the page.  As long as the page was 2,000 bytes or less, this would work well.

For larger pages, we need to use REQUEST_PIECES as follows:</b>


ops$tkyte@8i> 
ops$tkyte@8i> declare
  2      l_page    utl_http.html_pieces;
  3          l_url     varchar2(25) default
  4                            '
http://www.yahoo.com';
  5  begin
  6      l_page := utl_http.request_pieces( l_url,
  7                                         99999,
  8                                     <b>  'www-proxy' );</b>
  9  
 10      for i in 1 .. l_page.count
 11      loop
 12          p( l_page(i) );
 13                  exit when ( i = 3 );
 14      end loop;
 15  end;
 16  /
<html><head><title>Yahoo!</title><base href=
http://www.yahoo.com/ ><meta
http-equiv="PICS-Label"
content='(PICS-1.1 "
http://www.rsac.org/ratingsv01.html"
l gen true for "
http://www.yahoo.com" <code>r (n
0 s 0 v 0 l 0))'></head><body><center><form action=htt

[snip]....

href=r/sp>Sports</a>,
<a href=

PL/SQL procedure successfully completed.

Here we uses a proxy server (new in 8.1) to get the page since I am behind a firewall and we got the page back in an "array" -- not as a single variable...



Rating

  (69 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks

Pierre bilodeau, March 04, 2002 - 3:35 pm UTC

This example help me to use this function, thank you for your help

I have some problem.........

T.V. Sarada Priya, August 12, 2002 - 10:16 pm UTC

I am trying to use this UTL_HTTP in 8.1.7.2.0 Oracle Database on Compaq Tru64 Unix. But I always get an exception ORA6510: PL/SQL user defined unhandled error. After handling the error with an exception too, it doe not solve. Have I missed out any setting in Init.ORA or elsewhere? Otherwise, wanted to know whether this will work on Enterprise Edition or does it require any other extra Oracle products to be installed. I searched high and low in your web site as well as others, but could not find the right information. Please help me!!!
I used the same code u had given:

ops$tkyte@8i> declare
2 l_page long;
3 l_url varchar2(35) default
4 '
</code> http://aria.us.oracle.com/'; <code>
5 begin
6 l_page := utl_http.request( l_url );
7
8 p( l_page );
9 end;
10 /

Thanks

Sarada Priya

Tom Kyte
August 13, 2002 - 8:20 am UTC

Ummm, you do understand that aria is my server inside the Oracle firewall and that you won't be able to get to it?

That is the exception -- you are not able to get to the web site.

Try a URL that you can access and see what happens.

UTL_HTTP

debasis, November 20, 2002 - 5:09 pm UTC

good examples.

HTTP 407 Proxy Authentication Required

Rasin, July 05, 2003 - 3:18 am UTC

How do I avoid the above error

here is the code i am executing

declare
l_page utl_http.html_pieces;
l_url varchar2(25) default '</code> http://www.yahoo.com'; <code>
begin
l_page := utl_http.request_pieces( l_url,
99999,
'myproxy' );
for i in 1 .. l_page.count
loop
p( l_page(i) );
exit when ( i = 3 );
end loop;
end;
/

this proxy is in our firewall which requires windows NT username and password
can i pass my windows username and password for proxy authentication if not how can I pass username and password to my proxy server for authentication.

Thanks
Rasin

Tom Kyte
July 05, 2003 - 9:45 am UTC

given that you give me no idea what release -- all I can say is

in 8i, no go, you won't be doing this with utl_http. you'll have to load a java class or do it yourself using utl_tcp

in 9i, read the supplied packages guide -- there are lots of routines in utl_http that are new and allow you to set headers. including a "set authentication" routine that permits this.

I modified it

Rasin, July 05, 2003 - 3:37 am UTC

here is my latest code after modification
but still there is error
since out proxy uses windows username and password
for authentication i supplied my windows username
and password.

1 declare
2 req utl_http.req;
3 l_page utl_http.html_pieces;
4 l_url varchar2(25) default '</code> http://www.yahoo.com'; <code>
5 begin
6 UTL_HTTP.set_authentication(
7 req,
8 'my-windows-username',
9 'my-windows-password-mypass',
10 'Basic',
11 TRUE); -- true for proxy
12 --utl_http.set_authentication(req, username, password);
13 l_page := utl_http.request_pieces( l_url,
14 99999,
15 'proxawq' );
16 for i in 1 .. l_page.count
17 loop
18 p( l_page(i) );
19 exit when ( i = 3 );
20 end loop;
21* end;
archgn@AWQ8> /
declare
*
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1060
ORA-29261: bad argument
ORA-06512: at line 6


Tom Kyte
July 05, 2003 - 9:51 am UTC

I don't have a proxy server that requires authentication, so I cannot debug this for you. but you are not doing it properly. request and request_pieces are the legacy simplistic apis.

you need to read the examples in the supplied packages guide that show you how to use the more sophisticated apis. you need a begin_request call, set_* calls, get_response, read_line calls, end_response.


In fact:
</code> http://docs.oracle.com/docs/cd/B10501_01/appdev.920/a96612/u_http.htm#1012345 <code>

shows you verbaitim.


dumping url pages

A reader, December 05, 2003 - 3:04 pm UTC

Hi I have a requirement as follows:
1. Access a URL which requires me to enter user name
and password
2. Go to a particular set of pages that basically generates
a report dynamically
3. Dump the report into a directory for later viewing.

In your expert one on one, in utl_file, you have a
simple utility that does it.
I already have this application in a shell script
where I use wget but I am thinking that writing it
in pl/sql may simplify it - there is whole
lot of logic related to grabbing the session id
in shell script for reusing the same session as I go
to different urls).
As part of the requirement, I need
to also download some static images from the same
web application.

Can i Use utl_http for this? How would you do it?

thanx!!


Tom Kyte
December 05, 2003 - 4:59 pm UTC

in 9i you could, possibly. the images will be problematic (binary files).

i don't think it'd be any easier actually. not in this case.

Not unless you can "skip the web" altogether and just run the htp procedure and use my "dump_page" utility to save it out.

thanx tom!!

A reader, December 05, 2003 - 6:22 pm UTC


directory objects in wallet path?

Mark, May 25, 2004 - 1:50 pm UTC

Tom,

I am using utl_http.request_pieces on a 9.2.0.4 database to return data from an ssl. My question is, can I use a directory object name instead of a directory path in the wallet_path parameter?

Thanks.

Tom Kyte
May 25, 2004 - 2:30 pm UTC

don't know, haven't played with that ssl stuff too much myself.

A reader, July 20, 2004 - 10:50 am UTC

Hi Tom,
We are using UTL_HTTP to post message to a web server. We are using GET method. We specify character set we do post to web server
UTL_HTTP.SET_HEADER(req,'Content-Type','text/xml;charset=UTF-8');

After the post we receive response back from web server which stat the post is successfull or not. Web server response is SUCCESS or FAILED.....We are having problem here reading response from web server (SUCCESS or FAILED) as the return respose is in different character set. Our database character set and NLS_LANG parameter is WE8MSWIN1252.

Please help
Thanks

Tom Kyte
July 20, 2004 - 8:18 pm UTC

I'd sort of need a setup to reproduce as "we are having problem here reading response" is sort of "vague"

what *kind* of problem -- can you at least describe the issue?

A reader, July 21, 2004 - 9:37 am UTC

Hi Tom,

The question was I am having problem reading return value from response because of character set issue

Here is the code

DECLARE
req Utl_Http.req;
resp Utl_Http.resp;
NAME VARCHAR2 (255);
VALUE VARCHAR2 (1023);
v_message VARCHAR2 (32767);
replymessage VARCHAR2 (32767);
i number;
escaped_url VARCHAR2 (32767);
aa_msg varchar2(32767);
a VARCHAR2 (255);
message varchar2(32767);
c_outgoing_xml_message clob;
outgoing_xml_message sys.xmltype;
status varchar2(50);
ERROR_MESSAGE varchar2(50);
correlation varchar2(30):=0;
msgid raw(16):=null;
v_more_messages BOOLEAN := TRUE;
v_messages_dequeued number;
o_response varchar2(50):=null;
o_error varchar2(100):=null;
v_url varchar2(32767):= '</code> http://cw2k014215/eai_enu/start.swe?SWEExtSource=Inboundwf&SWEExtCmd=Execute&UserName=TEST&Password=TEST';
   v_msg     varchar2(32767) := '&SWEExtData=';
   new_message varchar2(32767);
   v_converted_message   varchar2(32767);
   
/************ BEGIN REUQEST ********************/   
PROCEDURE BEGIN_REQUEST IS
     BEGIN
     dbms_output.put_line('AT BEGIN REQUEST');
     req := Utl_Http.begin_request (escaped_url,  'GET');
     --dbms_output.put_line('request METHOD  '||req.METHOD);
    -- dbms_output.put_line('request VERSION '||req.http_version);
    -- dbms_output.put_line('request URL     '||substr(req.url,1,150));
     EXCEPTION
      WHEN Utl_Http.request_failed   THEN
           DBMS_OUTPUT.put_line ('Request_Failed: ' || Utl_Http.get_detailed_sqlerrm);
           Utl_Http.end_request (r => req); 
      WHEN Utl_Http.http_server_error  THEN
           DBMS_OUTPUT.put_line ('Http_Server_Error: ' || Utl_Http.get_detailed_sqlerrm);
           Utl_Http.end_request (r => req); 
      WHEN Utl_Http.http_client_error THEN
           DBMS_OUTPUT.put_line ('Http_Client_Error: ' || Utl_Http.get_detailed_sqlerrm);
           Utl_Http.end_request (r => req);  
      when others then
           dbms_output.put_line('ERROR AT BEGIN REQUEST '||SQLERRM);
           Utl_Http.end_request (r => req);
     END;   

/************ SET HEADER ********************/  
PROCEDURE SET_HEADER IS
     BEGIN
     dbms_output.put_line('AT SET HEADER');
     UTL_HTTP.SET_HEADER(req,'Content-Type','text/xml;charset=UTF-8');
     utl_http.set_follow_redirect(req,3);
     EXCEPTION
     when others then
        dbms_output.put_line('ERROR AT SET HEADER '||SQLERRM);
     END;
     
/************ GET_RESPONSE ********************/       
PROCEDURE GET_RESPONSE is
BEGIN
     dbms_output.put_line('AT GET RESPONSE');
     resp := Utl_Http.get_response (r => req);
    -- DBMS_OUTPUT.put_line ('Status code  : ' || resp.status_code);
    -- DBMS_OUTPUT.put_line ('Reason phrase: ' || resp.reason_phrase);
     EXCEPTION
     when others then
        dbms_output.put_line('ERROR AT GET RESPONSE '||SQLERRM);
     END; 
     
/************ GET_HEADER ********************/          
PROCEDURE GET_HEADER is
     BEGIN
     dbms_output.put_line('BEGIN OF GET HEADER ');
     FOR i IN 1 .. Utl_Http.get_header_count (r => resp)
     LOOP
        Utl_Http.get_header (r => resp, n => i, NAME => NAME, VALUE => VALUE);
      --  DBMS_OUTPUT.put_line ('HEADER : ' ||NAME || ': ' || substr(VALUE,1,200));
     END LOOP;
     dbms_output.put_line('END OF GET HEADER ');
      EXCEPTION
     when others then
        dbms_output.put_line('ERROR AT GET HEADER '||SQLERRM);
     END;
     
/************ READ_RESPONSE ********************/          
     
PROCEDURE READ_RESPONSE is
BEGIN
 LOOP
        dbms_output.put_line('in read message ');
        utl_http.read_line(resp,v_message,false);
        dbms_output.put_line('LENGTH OF REPly message '||length(v_message));
        dbms_output.put_line('v_msg '||v_message);

***********************************
************************************
I am having probem at (resp,v_message,false);

below is the output

in read message 
LENGTH OF REPly message 97
v_msg ÿþE <<-------- It should retuen SUCCESS or FAILED
                      
I am having problem here converting character set..I used convert function but no success


************************************
**********************************************************




       -- v_converted_message := convert(v_message, 'UTF-16','UTF-8');
       -- dbms_output.put_line('v_converted_message '||v_converted_message);
        i := 1;
        while (i < length(v_message)+1) 
        Loop
         if ascii(substr(v_message,i,1)) = 0 or 
            ascii(substr(v_message,i,1)) > 128 then
               null;
         else     
            new_message := new_message ||substr(v_message,i,1);
         end if; 
        i := i + 1;
        end loop;  
        dbms_output.put_line('NEW MESSAGE '||substr(new_message,1,200));
        dbms_output.put_line('LENGTH OF NEW MESSAGE '||length(new_message));
  
      END LOOP;
EXCEPTION
        WHEN Utl_Http.end_of_body THEN
              dbms_output.put_line('UTL HTTP END Of BODY ');  
        when others then
              dbms_output.put_line('ERROR AT READ RESPONSE '||SQLERRM);    
END;     
     
/********** MAIN PROCESS ***************/             
BEGIN
     While (v_more_messages)
     LOOP
     dbms_output.put_line('-------------------------------- ');
     BEGIN
        v_url  := '
http://cw2k014215/eai_enu/start.swe?SWEExtSource=Inboundwf&SWEExtCmd=Execute&UserName=TEST&Password=TEST'; <code>
v_msg := '&SWEExtData=';
outgoing_xml_message := null;
c_outgoing_xml_message := null;
new_message :=null;
pk_AQ.dequeue_message('SIEBEL_QUEUE', 'SIEBEL' , outgoing_xml_message ,
status , ERROR_MESSAGE, correlation, msgid , v_more_messages ,
v_messages_dequeued);
if v_more_messages = False then
exit;
end if;
dbms_output.put_line('msgid '||msgid);
dbms_output.put_line('STATUS '||status);
dbms_output.put_line('ERR MESSAGE '||ERROR_MESSAGE);
select outgoing_xml_message.getclobval()
into c_outgoing_xml_message
from dual p;

v_msg := v_msg||c_outgoing_xml_message;
message := v_url||v_msg;
dbms_output.put_line('length '||length(message));
escaped_url := utl_url.escape(message , false, 'UTF-8' );

/* request that exceptions are raised for error Status Codes */
Utl_Http.set_response_error_check (ENABLE => false );

/* allow testing for exceptions like Utl_Http.Http_Server_Error */
Utl_Http.set_detailed_excp_support (ENABLE => TRUE );

BEGIN_REQUEST;
set_header;
get_response;
get_header;
read_response;

if new_message = 'SUCCESS' then
COMMIT; -- MESSAGE WAS SUCCESSFULLY ABSORBED BY SIEBEL
ELSE
ROLLBACK;
-- UPDATE THE RETURNMESSAGE TAG WITH ERROR MESSAGE
PK_AQ.Update_ReturnMessage_tag('siebel_queue_table' , msgid , new_message , o_response , o_error);
end if;
Utl_Http.end_request (r => req);
Utl_Http.end_response (r => resp);

EXCEPTION
when others then
dbms_output.put_line(sqlerrm||' ERROR in DEQUEUE CALL ');
v_more_messages := false;
Utl_Http.end_request (r => req);
Utl_Http.end_response (r => resp);
end;
end loop;
rollback;
END;


/

Tom Kyte
July 21, 2004 - 10:55 am UTC

again, i see a bunch of code but no description at all about "what the problem is"

please -- describe the problem you are having.

I do not have your environment, won't be able to run your code -- but perhaps if you give us an idea "what is wrong"......

Modify "utl_http.request_pieces"

Robert, October 28, 2004 - 6:05 pm UTC

hi Tom,
Is it possible to turn "utl_http.request_pieces" into my own version so that it recognizes newline(chr(10)) and populate the array one line of data at a time

Specifically when grabbing csv data from yahoo like below:
</code> http://ichart.finance.yahoo.com/table.csv?s=APA&d=9&e=28&f=2004&g=d&a=0&b=4&c=2000&ignore=.csv <code>

thanks

Tom Kyte
October 28, 2004 - 7:59 pm UTC

you cannot modify utl_http, no.

you can certainly write you own routine that calls request pieces and parses it up any way you like tho

Global array or function returning array

robert, October 29, 2004 - 2:10 pm UTC

>you cannot modify utl_http, no.
>you can certainly write you own routine that calls request >pieces and parses it up any way you like tho

Thanks Tom, exactly what I did.
Only that I have to code to avoid blowing LONG type size (8i)
(l_page := utl_http.request_pieces( l_url, 16); )

Now any difference in terms of performance
whether I parse directly into a GLOBAL dbms_sql.varchar2_table variable or via function assignment ? like:

FUNCTION collect_data_fr_url
RETURN dbms_sql.varchar2_table ;

thanks

Tom Kyte
October 29, 2004 - 5:03 pm UTC

don't know what you mean by that last paragraph exactly.


storing XML in BLOB or XMLTYPe using utl_http

Taha, June 29, 2005 - 11:14 am UTC

Tom,

I have a java servlet which creates an XML document. Can I use UTL_HTTP from pl/sql to execute the servlet and store the XML into a blob or XMLTYPE column.? Could I do this ?


SQL> select UTL_HTTP.REQUEST( '
http://servername/servlet/pTestServlet?p1=data.txt'
) from dual; 


I beleive this function returns up to the first 2000 bytes of data retrieved from the given URL. How about if the size of my XML doc is more than 2000 bytes?

Thank you 

Tom Kyte
June 29, 2005 - 11:37 am UTC

you would never store XML in a blob, a clob maybe but not in a blob. a blob is for binary data, xml is specifically text.

utl_http has a much larger api that gives you complete access to the data -- see the docs, lots more than just request in there.

storing XML in BLOB or XMLTYPe using utl_http

Shweta, July 06, 2005 - 2:12 pm UTC

Hello 
I have following error while executing the sample code given. Please tell me the possible reason of the same.
thanks and regards
ShwetaA.

SQL> declare
  2  l_page    utl_http.html_pieces;
  3  l_url     varchar2(25) default '
http://www.yahoo.com';
  4  begin
  5  l_page := utl_http.request_pieces( l_url,
  6                                     8080,
  7                                    '
http://webproxy.ssmb.com'
);
  8  
  9      for i in 1 .. l_page.count
 10      loop
 11          p( l_page(i) );
 12                  exit when ( i = 3 );
 13      end loop;
 14  end;
 15  /
declare
*
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1665
ORA-12541: TNS:no listener
ORA-06512: at line 5 

Tom Kyte
July 06, 2005 - 2:32 pm UTC

is 8080 your port? the parameter you are passing it to is "max_pieces",

8080 is a pretty standard port number
8080 would be a strange choice for max pieces

therefore, I'd suggest you put the port into the proxy where it goes...

question on secure sites

A reader, August 23, 2005 - 1:51 pm UTC

I'm trying to read from an url that begins </code> https://, <code>but the sample code is throwing this error:

ORA-28871: certificate chain is incomplete

Can utl_http be used for secure sites? Can you provide an example, or point me to one? Thanks!

Tom Kyte
August 24, 2005 - 8:40 am UTC

do you have access to expert one on one Oracle? complete example in the appendix on UTL_HTTP using SSL.

utl_http

Elise, September 08, 2005 - 3:50 pm UTC

Your write up in Expert One-on-One was extremely helpful for setting up utl_http to use https. Thank you for sending me to that reference. And a big THANKS for writing that reference.

Tom Kyte
September 08, 2005 - 6:03 pm UTC

Actually, that tidbit - utl_http+https -- was written by David Knox (he and Joel Kallman contributed to the book - Joel did the Oracle text material, both are credited)

Click links I like above and you can check out Davids book on Oracle security.

UTL_HTTP error

Alan, December 01, 2005 - 1:54 pm UTC

I am running the UTL_HTTP sample from the link below:

</code> http://www.oracle.com/technology/tech/webservices/htdocs/samples/dbwebservice/DBWebServices_PLSQL.html <code>

I am running the Barnes book price lookup example. However, when I call the function get_price, I am getting the following error.

ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1022
ORA-12545: Connect failed because target host or object does not exist

I tried connecting to the server address listed in the example directly and it is up and accepting SOAP requests. Can you give me some direction in solving the issue?

Tom Kyte
December 02, 2005 - 9:55 am UTC

can the database server you are using connect to that machine or is it more firewalled than you and do you perhaps have a proxy server setup on your client machine that the database server isn't using?

TNS Error

Alan, December 02, 2005 - 10:51 am UTC

Thanks for the response.

Yes, there is a proxy that sits between the server and the internet. I tried adding the Proxy address, and the other error is now gone but I have a new one.

I added the following line.
utl_http.set_proxy('[my proxy ip]', NULL);

The error that I am getting is:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1022
ORA-12541: TNS:no listener

Could you please give me some direction on this one?

Tom Kyte
December 02, 2005 - 11:28 am UTC

can you break it down to a tiny example (i find that when I take a big example and distill it to just the relevant bit - I usually find my own mistake). That is, rather than work through a big big example, lets just get your utl_http simply talking "out".

ThJ, December 08, 2005 - 1:36 pm UTC

Thanks for all this information. But I wonder if the utl_http.set_proxy works.

If I set the proxy with utl_http.set_proxy and doesn't specify it in utl_http.request, the request won't work. But if I specify the proxy in the utl_http.request it works! Do we need to specify something else on the DB server ?

On the other hand, do I have to use the new uri datatypes to retrieve intranet file or there are no advantages ?
thanks

ThJ, December 09, 2005 - 3:54 am UTC

I forgot to specify that I'm running a 9.2.0.1.0 Oracle DB on a windows 2000.

For Alan, I could say that I receive the same message when I forgot to give the port number of the proxy

Tom Kyte
December 09, 2005 - 6:48 am UTC

give me the smallest possible example of what you are doing, both cases (the one that works and the one that does)

o small
o yet 100% complete
o but concise


Proxy usage

ThJ, December 22, 2005 - 9:25 am UTC

case a,c, d : OK
case b: error
case B : ERROR -29273***ORA-29273: HTTP request faile
ORA-06512: at
"SYS.UTL_HTTP", line 1556
ORA-12545: Connect failed because target host or

DECLARE
req utl_http.req;
resp utl_http.resp;
value VARCHAR2(4000);
proxy1 varchar2(2500) := 'myProxy:port';
p2 varchar2(2500) := 'list of bypass';
l_page utl_http.html_pieces;
l_url varchar2(25) default '</code> http://www.yahoo.com'; <code>
w1 varchar2(2500);
w2 varchar2(2500);

Begin

utl_http.set_proxy(proxy1,p2);
utl_http.get_proxy(w1,w2);

BEGIN
-- A With direct request : intranet
value := utl_http.request(' intranet address ');
dbms_output.put_line('Case A response '||substr(value,1,150));
EXCEPTION
WHEN others THEN dbms_output.put_line('case A : ERROR');
END;

BEGIN
-- B With direct request : internet
value := utl_http.request(l_url);
dbms_output.put_line('Case B response '||substr(value,1,150));
EXCEPTION WHEN others THEN
w1:=sqlcode ||'***'||substr(sqlerrm,1,150);
dbms_output.put_line('case B : ERROR '||w1);
END;

BEGIN
-- C With direct request : internet
value := utl_http.request(l_url,proxy1);
dbms_output.put_line('IntERnet request2 '||substr(value,1,200));
EXCEPTION WHEN others THEN
w1:=sqlcode ||'***'||substr(sqlerrm,1,150);
dbms_output.put_line('case C : ERROR '||w1);
END;

BEGIN
-- D With request pieces : internet
l_page := utl_http.request_pieces( l_url, 99999, proxy1 );
for i in 1 .. l_page.count
loop
p( l_page(i) );
exit when ( i = 1);
end loop;
EXCEPTION
WHEN others THEN dbms_output.put_line('Case D: ERROR');
END;

Tom Kyte
December 22, 2005 - 11:00 am UTC

utl_http.request is the old legacy api from 8.0 provided for backwards compatibility. it has a parameter to tell it what proxy to use.

utl_http dependencies in 9i

Mobra, May 11, 2006 - 4:58 am UTC

Is UTL_HTTP (and UTL_TCP and UTL_SMTP) dependent on Java in the database in 9i, or is it a "native" implementation?

And what about 10g?

Tom Kyte
May 11, 2006 - 7:22 pm UTC

as I recall - in 9i, they were in "C".

how to create a html link in oracle forms

reader, June 16, 2006 - 6:41 pm UTC

Tom,

Is there a function in oracle forms to create a html link just like <a href="asktom.oracle.com"> ? requirement is to show a link for Help which is a HTML page retrieved from Linux server

any PL/SQL package like utl_http that can be used ?

Tom Kyte
June 16, 2006 - 7:24 pm UTC

there is a "web page.show" sort of routine - but since it has been well over 10 years since I've actually touched forms....

I'll recommend otn.oracle.com -> discussion forums and the forms group.

It worked. Thanks for the tip.

Reader, June 21, 2006 - 6:59 pm UTC

Tom,

I was able to use the following command for linking the html.

begin
WEB.SHOW_DOCUMENT ('javascript:window.open("</code> http://www.google.com","","fullscreen=no,titlebar=no,location=no,toolbar=no,menubar=no,status=no,resizable=yes" <code>;self.close()','_blank');
end;

I know you would be of a real help.


utl_http

sam, August 27, 2006 - 10:36 pm UTC

Tom:

1. Can i use utl_http for load testing? I need to see when one user access a page how long it takes and when 10 users log in simultaneously how much more it will take?

2. I wrote a small program to do load testing. It bascially does that. However, I am thinking that is not accurate test because each call to utl_http waits for the first one to end and that is not actually a load test since it the URL call has to be executed simltaneously. Is this correct and is there a way to fix that you create one pl/sql program that does simultaneous calls.

url(1):= '</code> http://www.xxx';
url(2):= '
http://www.yyy';
url(3):= '
http://www.zzz'; <code>

start_time :=dbms_utility.get_time;

for i in 1..3 loop
for j in 1..10 loop --10 users
html := utl_http.request_pieces(url(i));
end loop;
end loop;
end_time(i):= dbms_utility.get_time;

Tom Kyte
August 28, 2006 - 8:43 am UTC

1) you can use utl_http for anything you want. But, if you are using the database to drive your load test (eg: your plsql is calling out to http and that is just calling back into the same database), that might tend to "skew" things.

2) that is why you run more than one at a time in a load test. you would use many sessions, each flinging URL's.


I would google load testing tools and get something you can script and run externally from the database under test.

UTL_HTTP

Chris, October 16, 2006 - 10:50 am UTC

Tom,
I notice that in the generated procedures for PSP, if you include an error page, a generic WHEN OTHERS is used that calls htp.init then th stored procedure for the error page. I did not even see htp.init in the documentation so I've done a little digging. The reason I'm asking is that I would like to be able to redirect to another page from the middle of a PSP. I am using owa_util.redirect_url in the cases where I want to do this, but you can only do that if you have not started writing the page yet. Obviously if I'm in a PSP I have already started writing the page. I was wondering if I could call htp.init, then owa_util.redirect_url to accomplish this.

Also, this may seem like a strange question, but is there a way I can stop all processing at that point so it no longer steps through the PL/SQL code. Obviously there are ways, but I'm looking for the best way. In ASP you can issue a Response.End and it stops generating the page (the response) as well as the procedural code that might come after it.

My application has a set of packaged procedures that I call to go to various pages. Any time I call one of these procedures in my code I follow it with a "RETURN" so that no further code is processed. Just seems kind of bulky, would you suggest anything different?

Tom Kyte
October 16, 2006 - 11:11 am UTC

owa.get_page would be "safer" I think, empty the page and throw it away.

utl_http

sridevi, February 08, 2007 - 6:20 am UTC

Hi this is Sridevi,
thank you tom for your valuable responses.But i have the problem regarding this
I am using utl_http.request_pieces(url_i) to see the content.

FUNCTION SF_URL_STRING_CHK(url_i IN varchar2 DEFAULT ' http://' )RETURN pls_integer IS
l_page utl_http.html_pieces; --For HTTP request open page
li_string_count PLS_INTEGER;
c_status_live CONSTANT PLS_INTEGER :=0; --Product is live
c_status_notlive CONSTANT PLS_INTEGER :=1; --Product is not live
c_search_string1 CONSTANT varchar2(25):='/pd3'; ---Search string for website found or not
c_search_string2 CONSTANT varchar2(150):= '/includes/Templates/Active/images/';
BEGIN
dbms_output.put_line('URL in '||url_i);
l_page := utl_http.request_pieces(url_i);
li_string_count := 0;
FOR i IN 1 .. l_page.count
LOOP
IF instr( l_page(i),c_search_string1)<>0
OR instr( l_page(i),c_search_string2)<>0 THEN
li_string_count := 1; ---Search
END IF;
EXIT WHEN ( i = 3 );
END LOOP;
IF li_string_count = 1 THEN
RETURN c_status_live; -----Product is
ELSE
RETURN c_status_notlive; -----Product is notlive
END IF;
EXCEPTION
WHEN Utl_Http.request_failed THEN
DBMS_OUTPUT.put_line ('Request_Failed: ' || Utl_Http.get_detailed_sqlerrm);
WHEN Utl_Http.http_server_error THEN
DBMS_OUTPUT.put_line ('Http_Server_Error: ' || Utl_Http.get_detailed_sqlerrm);

END SF_URL_STRING_CHK;


Right now my problem Is:
I am running like this
DECLARE
A PLS_INTEGER;
begin
for i in (select sl_no,url from edrprpt.test) loop
a:=PK_EDRP_PRODUCT_STATUS.SF_URL_STRING_CHK(i.url);
dbms_output.put_line(i.sl_no||'URL-->'||i.url||'status is-->'||a);
end loop;
end;

Form test table I am sending so many URL¿s .
If I am running ,sometimes it¿s giving following error for some of the URLS:
Request_Failed: ORA-12543: TNS:destination host unreachable.
Again if i will run the above ,some of the above URL's showing as correct or some of other are showing as the above problem.
It's showing inconsitent results,i thought that if any heavy images are there,then the URL taking time and it's showing the above error i think so.
Some times websites showing as DOWN,even the website is UP.

Please suggest me how to avoid this.It¿s urgent for me.Should I include anything????

Thank you,
Sridevi
Tom Kyte
February 08, 2007 - 8:29 am UTC

at the time you ran this, I would say that in fact the web site you were trying to contact in fact did not respond. By the time you looked at the website to verify it was "up or down", it was back up.

Sridevi, February 12, 2007 - 1:34 am UTC

Hi,

tell me how to do solve this problem....


Thaks in advance
Sridevi
Tom Kyte
February 12, 2007 - 10:33 am UTC

be patient?

did you read my response??

Sridevi, February 13, 2007 - 2:08 am UTC

Hi,

I read your response properly.But even when you run the procedure after seeing the website is up also you can fine out that oracle procedure will give the down only.That's the my problem.If we try (10 times)to check whether website is UP or Down through oracle built-in,then it is showing as UP.

Please help me to solve the problem.

thank you,
Sridevi

HI

Marcelo, June 26, 2007 - 3:39 pm UTC

Hi Tom,
I'm using the following code
 l_r_req utl_http.req;
 l_r_resp utl_http.resp;


in a package body, but I get the following error:
PLS-00302: component 'REQ' must be declared
PLS-00302: component 'RESP' must be declared


The base version is : 8.1.7.4
And I'm don't know how to get this done... could you help me ??

Regards
Marcelo

Tom Kyte
July 02, 2007 - 9:50 am UTC

way back in the dark ages of 8i
http://docs.oracle.com/docs/cd/A87860_01/doc/appdev.817/a76936/utl_http.htm#998100

utl_http was very simple.

and it did not have these capabilities.

how to get this done? get onto current supported versions....

Hopefully useful to someone...

andrew, August 03, 2007 - 4:58 pm UTC

Hopefully this is of use to others - I needed to parse a UTL_HTTP.request_pieces response into lines. Based on snippets of your code of course...

CREATE OR REPLACE function clob2line (p_clob in clob)
return row_type pipelined
is
-- create or replace type ROW_TYPE as table of varchar2(4000)
l_last number;
l_current number;
begin
l_last := 1;
while l_last <= dbms_lob.getlength(p_clob) loop
l_current := dbms_lob.instr( p_clob, chr(10), l_last, 1 );
exit when (nvl(l_current,0) = 0);
-- chr(13)||chr(10) is set of characters in rtrim. handles LF or CRLF
pipe row (rtrim(dbms_lob.substr( p_clob, l_current-l_last+1,l_last ), chr(13)||chr(10)));
l_last := l_current+1;
end loop;
-- remaining piece if no newline at end. cleanup to be safe
pipe row (rtrim(dbms_lob.substr( p_clob, 32767, l_last ), chr(13)||chr(10)));
return;
end;
/
CREATE OR REPLACE FUNCTION http2clob (p_url VARCHAR2)
RETURN CLOB
IS
l_pieces UTL_HTTP.html_pieces;
l_clob CLOB;
BEGIN
l_pieces := UTL_HTTP.request_pieces (p_url);
DBMS_LOB.createtemporary (l_clob, TRUE);
DBMS_LOB.OPEN (l_clob, DBMS_LOB.lob_readwrite);

FOR i IN l_pieces.FIRST .. l_pieces.LAST
LOOP
DBMS_LOB.writeappend (l_clob, LENGTH (l_pieces (i)), l_pieces (i));
END LOOP;

DBMS_LOB.CLOSE (l_clob);
RETURN l_clob;
END;
/

column column_value format a60

select rownum, column_value
-- confirm junk CR/LF cleaned up
--, instr(column_value, chr(13)), instr(column_value, chr(10))
from table(clob2line(http2clob(' http://asktom.oracle.com' )))
where rownum < 5;


e.g.
ROWNUM COLUMN_VALUE
---------- ------------------------------------------------------------
1
2 <HTML>
3 <HEAD>
4 <title>Blah blah...</title>

Or just the easy way...

andrew, August 06, 2007 - 1:33 pm UTC

-- using read_line is much easier (and fine if network latency not a concern?)
create or replace function http2line (p_url in varchar2)
return ROW_TYPE pipelined
is
req utl_http.req;
resp utl_http.resp;
rv VARCHAR2(4000);
BEGIN
-- utl_http.set_proxy('proxy.my-company.com','corp.my-company.com');
req := utl_http.begin_request(p_url);
-- utl_http.set_header(req, 'User-Agent', 'Mozilla/4.0');
resp := utl_http.get_response(req);
LOOP
utl_http.read_line(resp, rv, TRUE);
pipe row (rv);
END LOOP;
utl_http.end_response(resp);
return;
EXCEPTION
WHEN utl_http.end_of_body THEN
utl_http.end_response(resp);
return;
END;
/

-- line by line result
select rownum, column_value from table(http2line(' http://asktom.oracle.com' ));

But what about sending content?

Peter, September 24, 2007 - 12:39 pm UTC

Hi,

I searched the internet for an answer to my question, and this discussion comes closest.
We are using the utl_http package to send and receive soap messages between several systems. It works fine. even (very) large soap messages can be read using a loop on utl_http.read_text with an end_response in the exception block.

example:
begin
  loop
    utl_http.read_text(l_http_response, l_response_line, 32767);
    l_incoming_soap_xml := l_incoming_soap_xml || l_response_line;
  end loop;
exception
  when utl_http.end_of_body then
    utl_http.end_response(l_http_response);
end;


But now I want to send a soap message bigger then 32k, using the utl_http.write_text procedure. Since a varchar2 parameter is used, the procedure is limited to 32k. Does anybody know if I can keep on using the write_text procedure in a loop until the entire message is gone? The message is stored in a clob, so I can tell the utl_http package the length of my soapmessage by using
utl_http.set_header( l_http_request, 'Content-Length', length( l_outgoing_soap_xml ));


Does anybody know if this is possible??? It is not easy for me to test this in our development environment, so it's better if I know for sure.

Thanks,
Peter
Tom Kyte
September 26, 2007 - 8:21 pm UTC

well, it should be TRIVIAL to test in your test environment!!! that is what *test* environments are (cough cough) FOR.

but, yes, you can
http://docs.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_http.htm#sthref14617

utl_http and upload file

A reader, June 05, 2008 - 12:26 pm UTC

Hi Tom,

I read all the messages in this thread, I didn't find an answer for my problem.

I have a 10.2 g oracle database. I want to use utl_http post method to call a web service. One of the parameter for this web service is a file. When I specified the filename, The reponse from the webservice is the file not found.
Any help is appraciated!

Thanks a lot!!

This is part of my code:
CREATE OR REPLACE procedure aps.post_test1
is

i_messagetext varchar2(32767) := 'IdcService=CHECKIN_NEW&dDocTitle=PRS&' ||
'dDocAccount=&dDocAuthor=Upload&dSecurityGroup=PUBLIC&dDocType=Report&xDivision=AES&'||
'xGroup=IS&primaryFile=file:/local/ORACLE/post_test1.prc';

i_urltext varchar2(32767) := ' https://kek.com/aps_trn2/idcplg';

i_timeout integer default 60;
o_replytext varchar2(32767) := null;
o_return_code integer := null;

sslreq utl_http.req;
rsp utl_http.resp;
replymessage varchar2(32767);
replyline varchar2(32767);
v_xml varchar2(32767);
crlf CONSTANT VARCHAR2(2) := chr(13) || chr(10);

begin

utl_http.set_wallet('file:/local/ORACLE/product/10.2.0/Db_1/WALLET','password');
utl_http.set_response_error_check(enable =>true);
utl_http.set_detailed_excp_support(enable =>true);

sslreq := utl_http.begin_request(i_urltext,'POST','HTTP/1.1');


utl_http.Set_Authentication (
r => sslreq,
username => 'test',
password => 'abc');


utl_http.set_header(sslreq, 'user-agent', 'mozilla/4.0');
utl_http.set_header(sslreq,'Content-Type','text/xml');
utl_http.set_header(sslreq,'Content-Length',to_char(length(i_messagetext)));
utl_http.set_transfer_timeout(i_timeout);

utl_http.write_text(sslreq,i_messagetext);

rsp := UTL_http.get_response(sslreq);

replymessage :='';


... ...

end post_test1;
/
Tom Kyte
June 05, 2008 - 1:37 pm UTC

so, is this web service running on the same machine as the database server.

A reader, June 05, 2008 - 1:53 pm UTC

No they are in different servers.

Thanks!
Tom Kyte
June 05, 2008 - 2:03 pm UTC

have you thought about that.

How would a web service - executing on machine1 - read a file residing on machine2?


if it could, would that not fit into some of the definitions of "a security problem", "a virus like feature"


You'd have to get the contents of the file to the web service now wouldn't you... The name of a file on a remote file system the web service cannot see or touch does not do it very much.

A reader, June 05, 2008 - 3:00 pm UTC

Do you know how to upload a file using utl_http? Do you have an example of this?

Thanks a lot!
Tom Kyte
June 05, 2008 - 5:43 pm UTC

you would have to encode it using the http protocol and put it "in the upload stream", just like you would with sending an email attachment for example.

What is the soap message supposed to look like that your web service is expecting, that would give you an idea of what you need to do.

None Unicode Character set

Hasith, August 13, 2008 - 5:40 am UTC

Hi,
I tried to use Utl_Http package to send a SOAP request. I set content type to UTF8 and use Set_Authentication to set username and password. My database character set is in WE8MSWIN1252. When my username or password containes none ascii characters, then Set_Authentication method converts those WE8MSWIN1252 characters to AL32UTF8 characters and request become failed. Becuase my SOAP server authenticate username and password based on the stored username and password on Oracle Database. So how can i insert WE8MSWIN1252 characters to my requests' authentication.?
Thanks

utl_http to upload clob data as file to remote server

P., August 28, 2008 - 6:44 am UTC

Hi Tom,

I'm trying to upload a text file which is in fact clob data in a table. I want for the remote server to be the same as uploading via html upload file page i.e. enctype=multipart/form-data, giving some file name(generated by some rule) and then write the clob data to be interpreted as the content of the file. Do you have an example how to achieve this?

l_http_req := utl_http.begin_request (' http://www.smth.com/upl', 'POST',utl_http.HTTP_VERSION_1_1);
utl_http.set_header(l_http_req, 'enctype','multipart/form-data');
--HOW TO SET THE FILE NAME HERE?
utl_http.write_text(l_http_req, c_data);
utl_http.end_request(l_http_req);


Tom Kyte
August 29, 2008 - 4:09 pm UTC

you'd have to do the same thing a browser does - I do not have an example of the multipart/form-data encoding, but it is a documented standard.

P., September 01, 2008 - 10:03 am UTC

Hi Tom,

you were right, here is the information that was useful for me:

========================
POST /path/to/script.php HTTP/1.0
Host: example.com
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: $requestlen

--AaB03x
content-disposition: form-data; name="field1"

$field1
--AaB03x
content-disposition: form-data; name="field2"

$field2
--AaB03x
content-disposition: form-data; name="userfile"; filename="$filename"
Content-Type: $mimetype
Content-Transfer-Encoding: binary

$binarydata
--AaB03x--
==========================

for more info: http://chxo.com/be2/20050724_93bf.html
and the doc here: http://www.faqs.org/rfcs/rfc2388.html

in 8i

nil, September 22, 2008 - 5:15 am UTC

What will be the equivalent code in Oracle 81.7.4 for following code.

DECLARE
req utl_http.req;
resp utl_http.resp;
value VARCHAR2(1024);

username VARCHAR2(20);
password VARCHAR2(20);

BEGIN

username := 'jsmith';
password := 'Sillybilly123';

-- Turn off checking of status code. We will check it by ourselves.
-- utl_http.http_response_error_check(FALSE);

req := utl_http.begin_request(' http://anakin.cellnet.co.uk/jsmith/ViewCustomer.php?msisdn=07801007142' );

utl_http.set_authentication(req, username, password);

resp := utl_http.get_response(req);



FOR i IN 1..utl_http.get_header_count(resp) LOOP
LOOP
utl_http.read_line(resp, value, TRUE);
dbms_output.put_line(value);

END LOOP;

utl_http.end_response(resp);

END LOOP;
utl_http.end_response(resp);
EXCEPTION
WHEN utl_http.end_of_body THEN
utl_http.end_response(resp);
END;
/

Tom Kyte
September 23, 2008 - 12:23 pm UTC

there isn't any - utl_http did not have those features/functions way back then. You'd have to look into

a) using utl_tcp and writing the http protocol yourself
b) using a java stored procedure.

Changes on UTL_HTTP in 11g

Jeff, January 11, 2009 - 1:08 am UTC

In 11g, access control was changed on UTL_HTTP. Metalink note 453786.1 gives a workqround. But the workaround only addresses PL/SQL block. If I call it in a procedure, it does not work. What is the additional to make it work in a procedure?  Thanks a lot.

SQL> create or replace procedure test_utl as
  2  l_url varchar2(32767);
  3  l_conn utl_http.req;
  4  BEGIN
  5  l_url := 'http://www.oracle.com';
  6  l_conn := utl_http.begin_request(url => l_url, method => 'POST', http_version=> 'HTTP/1.0');
  7  dbms_output.put_line('Anonymous Block Executed successfully');
  8  END;
  9  /

Procedure created.

SQL> exec test_utl;
BEGIN test_utl; END;

*
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1029
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "JY.TEST_UTL", line 6
ORA-06512: at line 1

Tom Kyte
January 12, 2009 - 8:36 pm UTC

be more clear please. show the 'workaround' you refer to - that is a rather large note and I'm not at all sure what you are referring to.

UTL_HTTP issue

Jeff, January 13, 2009 - 10:34 am UTC

After a fresh installation of 11g, the following code does not work:

SQL> set serveroutput on
SQL> DECLARE
 l_url varchar2(32767);
 l_conn utl_http.req;
 BEGIN
 l_url := 'http://www.oracle.com';
 l_conn := utl_http.begin_request(url => l_url, method => 'POST', http_version=> 'HTTP/1.0');
 dbms_output.put_line('Anonymous Block Executed successfully');
END;
/

The error is ORA-24247: network access denied by access control list (ACL)

The workaround is to grant the user the CONNECT to a XML file, using the code provided in 453756.1. This workaround works for above code. But if I put above code into a procedure, the ORA-24247 shows again:

SQL> create or replace procedure test_utl as
    l_url varchar2(32767);
    l_conn utl_http.req;
    BEGIN
    l_url := 'http://www.oracle.com';
    l_conn := utl_http.begin_request(url => l_url, method => 'POST', http_version=> 'HTTP/1.0');
    dbms_output.put_line('Anonymous Block Executed successfully');
END;
/

Procedure created.

SQL> exec test_utl;
BEGIN test_utl; END;

*
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1029
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "JY.TEST_UTL", line 6
ORA-06512: at line 1



utl_http to upload clob data as file to remote server

Zeev, June 04, 2009 - 11:02 am UTC

I am newbie to all the HTTP_POST issue.
I've followed the steps posted on this thread and did completed the process which runs without errors, BUT now the client wants me to implement it using "multipart/form-data" which I have no idea what to do with.
He uses a manual process as follow: <div id="main">
<form action="upload" enctype="multipart/form-data" method="post">
<div id="upload">
<input type="hidden" name="verbose" value="yes" />
<h3 style="font-weight:bold; color:#054bbb">Submit
file</h3><br/>
<input type="file" size="70" name="xmlfile" /><br/>
<table>
<tr>
<td align="left">Request Log Level:</td>
<td align="left">
<select name="logLevel">
<option value="" selected>Default</option>
<option value="D">Debug</option>
<option value="I">Info</option>
<option value="W">Warn</option>
<option value="E">Error</option>
<option value="F">Fatal</option>
</select>
</td>
</tr>
<tr>
<td align="left">Check import file for errors
without
actually processing it:</td>
<td align="left"><input type="checkbox"
name="validate"
value="true"></td>
</tr>
<tr>
<td align="left">Verbose mode:</td>
<td align="left"><input type="checkbox"
name="verbose"
value="true"></td>
</tr>
</table>
<input type="submit" value="Submit" />
</div>
</form>
</div>. Please advise me what can I do and where to start.
Kind Regards


Tom Kyte
June 08, 2009 - 11:33 am UTC

You'd have to research (there are many sources out there) what the encoding style of that particular one is (it sends the http request in much the same format as email with attachments is sent). And then implement it.


http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

We don't do that out of the box - you could use java stored procedures as well, java has some api's for doing such a thing (but again, research project, I don't have the code)

utl_http for webservice

Nantha, October 01, 2009 - 2:26 pm UTC

Tom,

I'm able to define a request, and get an answer from the webservice. Problem is in receiving the xmldocument.
For small answers no problem, but they can get over 32676 chars.

When I say
http_resp := utl_http.get_response(http_req);
utl_http.read_text(http_resp, env);
resp.doc := xmltype.createxml(env);

It is not reading the whole xml and so it is not able to create an xmltyle.
I tried CLOB, even clob brings only 32676 chars.
Could you please let me know if there is another way to do it?
Thanks,
Nantha


Tom Kyte
October 07, 2009 - 2:52 pm UTC

keep calling read_text until you get an exception utl_http.end_of_body

UTL_HTTP access denied by ACL in a procedure

Paul, November 16, 2009 - 9:37 am UTC

Similar problem to "Jeff from AZ USA".

I can execute a simple utl_http request in an anonymous block. When I use the same code and url in a stored procedure I receive the error "network access denied by ACL" ORA-06512.

The user running the block is the same as the schema of the compiled procedure.

Has anyone found a solution for this?

Error ORA-24247

Paul, November 16, 2009 - 10:55 am UTC

The true error is ORA-24247.

I have set up an access control list and granted connect priviledges to the account the owns the procedure.

It is an internal web server I am trying to connect to. I have tried by name and by ip address.

In an anynonymous block I can connect to external sites even without using using assign_acl for the host.


Tom Kyte
November 23, 2009 - 12:13 pm UTC

roles are not enabled in a definers rights stored procedure.

If you would set up the ENTIRE example, we can take a look at it. Like I do - give a COMPLETE soup to nuts example to work with.

A reader, December 09, 2009 - 11:22 pm UTC

Tom,

I am using sample snippet below from your first post, but the problem i am facing is the website has javascript embedded and I am not able to view the contents(output) generated from it, do i have to pass in a some separate parameters to view that data as well?

Not a web programmer having difficulties with this simple task.

Thanks.

declare
l_page utl_http.html_pieces;
l_url varchar2(250) default ' http://www.samplesite.com';
begin

l_page := utl_http.request_pieces( l_url);
for i in 1 .. l_page.count
loop
dbms_output.put_line( l_page(i) );
exit when ( i = 3);
end loop;
end;
/

Tom Kyte
December 10, 2009 - 2:34 pm UTC

javascript runs on a client, in a javascript "virtual machine"

you'd have to write a javascript virtual machine if you wanted to retrieve HTML and have the javascript run.

Authentication with PasswordDigest in the Password Type

Ernesto, January 19, 2010 - 6:37 pm UTC

Hi Tom:

We´re trying to make a request with the "PasswordDigest" password type using the utl_http.set_authentication method. Can you tell us if that's possible with UTL_HTTP package?

Thanks a lot.

A reader, June 22, 2011 - 2:02 am UTC

Dear,
i created a procedure to send http request to an sms gateway company so to send messages -sms, when running the proceuder i have this error:
Connecting to the database sms.
Http_Client_Error: ORA-29268: HTTP client error 407 - Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
Process exited.

my proceudr :
create or replace
PROCEDURE UTL_HTTP_REQUETS AS

REQ UTL_HTTP.REQ;
RESP UTL_HTTP.RESP;
NAME VARCHAR2 (500);
VALUE VARCHAR2 (1023);
V_MSG VARCHAR2 (500);
V_URL VARCHAR2(32767) := 'http://mps/HTTPInterface/Send.aspx'||'?'||'UserName=USER'||'&'||'Password=PASSWORD'||'&'||'Recipients=00966543681088'||'&'||'MessageText=062806330645002006270644064406470020062706440631062D064506460020062706440631062D064A0645'||'&'||'ClientMessageID=5'||'&'||'SenderName=mohannad'||'&'||'MessageType=unicode';


my_scheme VARCHAR2(256);
my_realm VARCHAR2(256);
MY_PROXY BOOLEAN;

--v_url VARCHAR2 (32767) := ;

--http://mps.egyptsms.com/HTTPInterface/Send.aspx?UserName=Soroof'||'&'||'Password=Sor00f77'||'&'||'Recipients=966543681088'||'&'||'essageText=062806330645002006270644064406470020062706440631062D064506460020062706440631062D064A0645'||'&'||'ClientMessageID=5'||'&'||'SenderName=mohannad'||'&'||'MessageType=unicode

BEGIN


/* request that exceptions are raised for error Status Codes */
UTL_HTTP.SET_RESPONSE_ERROR_CHECK (ENABLE => TRUE );

/* allow testing for exceptions like Utl_Http.Http_Server_Error */
UTL_HTTP.SET_DETAILED_EXCP_SUPPORT (ENABLE => TRUE );


UTL_HTTP.SET_PROXY (
PROXY => 'http://192.168.14.99:8080',
NO_PROXY_DOMAINS => 'soroof.com'
);


REQ := UTL_HTTP.BEGIN_REQUEST (URL => V_URL, METHOD => 'GET');


/*
Alternatively use method => 'POST' and Utl_Http.Write_Text to
build an arbitrarily long message
*/


UTL_HTTP.SET_AUTHENTICATION (
R => REQ,
USERNAME => 'mhersh',
PASSWORD => 'alhershm',
SCHEME => 'Basic',
FOR_PROXY => FALSE --/* this info is for the target Web server
);




UTL_HTTP.SET_HEADER (R => REQ, NAME => 'User-Agent', VALUE => 'Mozilla/4.0,ie8,ie9,ie7,ie6');

RESP := UTL_HTTP.GET_RESPONSE (R => REQ);

DBMS_OUTPUT.PUT_LINE ('Status code: ' || RESP.STATUS_CODE);
DBMS_OUTPUT.PUT_LINE ('Reason phrase: ' || RESP.REASON_PHRASE);

FOR I IN 1 .. UTL_HTTP.GET_HEADER_COUNT (R => RESP)
LOOP
UTL_HTTP.GET_HEADER (R => RESP, N => I, NAME => NAME, VALUE => VALUE);
DBMS_OUTPUT.PUT_LINE (NAME || ': ' || VALUE);
END LOOP;



BEGIN
LOOP
UTL_HTTP.READ_TEXT (R => RESP, DATA => V_MSG);
DBMS_OUTPUT.PUT_LINE (V_MSG);
END LOOP;
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY
THEN
NULL;
END;

UTL_HTTP.END_RESPONSE (R => RESP);
EXCEPTION
/*
The exception handling illustrates the use of "pragma-ed" exceptions
like Utl_Http.Http_Client_Error. In a realistic example, the program
would use these when it coded explicit recovery actions.

Request_Failed is raised for all exceptions after calling
Utl_Http.Set_Detailed_Excp_Support ( ENABLE=>FALSE )
And it is NEVER raised after calling with ENABLE=>TRUE
*/
WHEN UTL_HTTP.REQUEST_FAILED
THEN
DBMS_OUTPUT.PUT_LINE (
'Request_Failed: ' || UTL_HTTP.GET_DETAILED_SQLERRM
);
/* raised by URL http://xxx.oracle.com/ */
WHEN UTL_HTTP.HTTP_SERVER_ERROR
THEN
DBMS_OUTPUT.PUT_LINE (
'Http_Server_Error: ' || UTL_HTTP.GET_DETAILED_SQLERRM
);
/* raised by URL http://otn.oracle.com/xxx */
WHEN UTL_HTTP.HTTP_CLIENT_ERROR
THEN
DBMS_OUTPUT.PUT_LINE (
'Http_Client_Error: ' || UTL_HTTP.GET_DETAILED_SQLERRM
);
/* code for all the other defined exceptions you can recover from */
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE (SQLERRM);
END UTL_HTTP_REQUETS;

why the user name and password not valid in the proxy server? once i login by those username and password and rung the request in the browser it passed, please advice.
Tom Kyte
June 22, 2011 - 10:29 am UTC

because you TOLD us not to use it on the proxy and in fact, you commented it as such???

Your proxy requires authentication - in addition to the web site you are using

   UTL_HTTP.SET_AUTHENTICATION (
      R              => REQ,
      USERNAME       => 'mhersh',
      PASSWORD       => 'alhershm',
     SCHEME         => 'Basic',
     FOR_PROXY      => FALSE --/* this info is for the target Web server 
   );



if that is in fact your real password, please change it soon

A reader, June 25, 2011 - 1:15 am UTC

Dear sir,

i set the authintication for my proxy server
UTL_HTTP.SET_PROXY (
PROXY => 'usernamd:passwd@ http://192.168.14.99:8080'
);
but still i have the same problem:

Http_Client_Error: ORA-29268: HTTP client error 407 - Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
Process exited.

Tom Kyte
June 25, 2011 - 11:23 am UTC

utl_http.set_proxy(' http://username:passwd@192.168.14.99:8080' );

You have the username/password in the wrong place, it goes after the protocol.

A reader, June 26, 2011 - 12:59 am UTC

Dear sir,

it was by mistake written like this in the previous post, in my procedure its written as you mentioned: 'http:\\username:password@proxy:port' and still i have the same problem, iam using my username password in the domain which has a permission to access internet withen that proxy, is that enough or i need special authontications??

thanks
Tom Kyte
June 26, 2011 - 11:25 am UTC

http:\\username ???? really - \\? is that YAT (yet another typo)

how about this - write the SMALLEST possible block of code that just gets a simple external URL - no authentication required, just something really really really TINY and simple that contains the fewest possible UTL_HTTP calls you can make - and cut and paste that block and its output directly from a sqlplus screen. MAKE IT TINY - you don't need much at all in this block - just like a set proxy call and a get url type of call.

try also using set_authentication with for_proxy => true.

ORA-29268: HTTP client error 407

A reader, June 27, 2011 - 3:01 am UTC

Dear,
regareding the previous note:
i tried my code in another database server which is connected directly to the internet (no proxy), it passsed, put in the database server with proxy still i have the same error, i think this is from our proxy, is there any setting or special authontications for the user i put it in set_proxy??
thanks.
Tom Kyte
June 27, 2011 - 11:21 am UTC

did you try what I recommended above? set authentication, for proxy -> true

and do not put the user/pass in the proxy url

how to use utl_http package in oracle

K.V.Koteswara rao, August 17, 2011 - 2:09 am UTC

hi,
i have one requirement to use utl_http.By using this package i need to send one request to
webserver with required parameters based on that request that webserver gives response.i need to
get that response and populate those response details in my database table.so for this one please
give one example.it helps a lot to me

thanks in advance
thanks & regards,
k.v.koteswara rao.

Tom Kyte
August 17, 2011 - 4:15 am UTC

so, you fire this question off in multiple places, neat.

did you read the answer above?

UTL_HTTP returns code for Proxy

Ram, October 14, 2011 - 8:06 am UTC

Hi Tom,

I read the entire thread above and also read the entire documentaion for UTL_HTTP however still not getting what is going wrong :

I have following code :

DECLARE
x utl_http.html_pieces;
req utl_http.req;
resp utl_http.resp;
value VARCHAR2(1024);
i integer;
BEGIN
utl_http.set_proxy('my_proxy', NULL);

req := utl_http.begin_request(' http://www.oracle.com/' );
resp := utl_http.get_response(req);

i := 1;
LOOP
utl_http.read_line(resp, value, TRUE);
dbms_output.put_line(value);
exit when i = 5;
i := i + 1;

END LOOP;

/*
x := utl_http.request_pieces(' http://www.oracle.com/', 100);
dbms_output.put_line(x.count || ' pieces were retrieved.');
dbms_output.put_line('with total length ');
IF x.count < 1
THEN dbms_output.put_line('0');
ELSE dbms_output.put_line
((2000 * (x.count - 1)) + length(x(x.count)));
END IF;
*/

utl_http.end_response(resp);

EXCEPTION
WHEN utl_http.end_of_body THEN
utl_http.end_response(resp);

END;

The problem is when I run this , Instead of getting the content of GOOGLE.COM I'm getting the contents from my proxy displayed.

Also I did try everything including UTL_HTTP.request + Authentication ( virtually tried everything documented ) but still no luck.

I think I'm doing something wrong which I'm not able to figure out.

Is there a problem with my proxy settings? ( But I can connect to www.oracle.com through my machine )

also When I run the /* Commented */ stuff in the above code I get the output of number of pieces and the length etc but again I believe that info is for the proxy content only ( Not sure )

Is there something wrong?

Thanks in Advance,
Ram
Tom Kyte
October 14, 2011 - 11:23 pm UTC

what is the output from your proxy server, all of it(headers included). do you require a login to your proxy server?

Outout of Proxy

A reader, October 17, 2011 - 4:18 am UTC

Hi Tom,

Here is what I get after running the above code :

/Version02242003-21August2011
//<my_proxy_name>/rrweb/auto.proxy
function FindProxyForURL(url, host)
{
host = host.toLowerCase();
url = url.toLowerCase();
clientip = myIpAddress();
/* Section 10 Asia - Direct to US proxy */
if (shExpMatch(host, "<my_company_domain>") ||
shExpMatch(host, "<my_company_domain>") ||
.
.
.
.
And the series of IF conditions


Whichever URL I enter , I get the same output and when I opened the proxy URL in the browser , I found the same code.

Also I'm able to connect and open any site through my machine in browser window ( Which of cource gets routed through the proxy server ) but I do not enter any login details anytime. Is there a possibility to get my login credentials through my machine login ?

Regards,
Ram.
Tom Kyte
October 17, 2011 - 5:12 am UTC

that is not a proxy server, that is an automatic configuration script - it is used by the browser to see if it needs a proxy server or NOT for a given URL. But it is most definitely NOT a proxy server.

point it to your PROXY SERVER, not to the file that the browser would run to find out what proxy server to use or not to use.

find out from your admin what the name of your proxy server really is if you don't know it. (in that script somewhere you should be able to find something like:

return "PROXY host.name.here:80"


(the 80 might not be 80, could be some other number...)

that is your proxy server information.

Excellent!!!

Ram, October 17, 2011 - 7:48 am UTC

Hi Tom,

You are awesome !!!!!

Just after posting the review I was going through the Proxy Config file and then read your response and then searched like mad for the proxy server for my domain and eventually found one which works for me !!

Thanks a ton for your help.

Now one another thing that bothers me is "How to extract the table information form the given HTML code ? "

Meaning if you just go through following URL ( just an example ) :

http://psoug.org/reference/utl_http.html

You would find the information regarding UTL_HTTP is presented in TABLE format. All I want is to get this information to my database table ( say the Name,Data Type and Value columns from the web page ).

Is there a way to do that or I will have write my own parser kind of routine which would search for <TABLE> , </TABLE> tags or <td> <tr> tags in the source code that I have got through UTL_HTTP.BEGIN_REQUEST function?

Hope I have clarified my question in correct manner.

Thanks again for all your help !!

Regards,
Ram
Tom Kyte
October 17, 2011 - 8:39 am UTC

if the html is "well formed", you might be able to use some sort of xml parser - otherwise you have to sort of know what you are looking for and parse it accordingly.

so, in general - no not really - but if the html is well formed enough - various xml parsers (including ones in the database) would help you parse through it. You still have to know what you are looking for

utl_http for .MHT file

Praveen Ray, February 14, 2012 - 9:52 am UTC

Hi Tom,

I have saved a few hundreds of web pages in .MHT format. And I wanted to write a pl/sql code in order to read them and do some operation on the content and store in a table.

It is quite easy for the .TXT files with utl_file. What is the most preferable solution from your viewpoint for .MHT ones.

Thank you.


Praveen Ray
Tom Kyte
February 14, 2012 - 12:17 pm UTC

utl_file can read binary and text files, so you can process it - you'll just need to learn the file format for a MHT file.

Navigating thru Web pages

Praveen Ray, March 11, 2012 - 9:16 am UTC

Hi Tom,

So far, I have used UTL_HTTP package for a single URL and processed the data, but my requirement is to navigate through multiple Web-pages. For example, I declare ASk Tom Home page (i.e. http://asktom.oracle.com/pls/apex/f?p=100:1:0 ) in a variable and in the main block, use some logic to press the hyperlink (Click <here> to view your questions.) and there I'll supply me email ID in order to get the final pages which I need for my process.

This is just an example, I am not going to process that page anyway ;)

Praveen Ray
Tom Kyte
March 12, 2012 - 7:45 am UTC

so, what is your question exactly?


navigating thru web-pages

Praveen Ray, March 12, 2012 - 8:57 am UTC

I really need your help here:

My question is, one URL is fixed and known in advance, but depending on the parameters to that page redirects to different pages. Say for example, if I provide date1 then weather forecast site gives reportX and if I provide date2 then reportY...

So, please, take an example given earler with regard to Ask Tom site and help me write a pl/sql. really appreciated.

Thank you
Tom Kyte
March 12, 2012 - 9:31 am UTC

I don't know what you mean.


You couldn't possibly be asking me to code a routine that given a URL, will retrieve that URL and parse the return values finding and building all embedded URLS and then retrieving them and so on - could you???? Please say "no, that is not what I meant"


If you are asking that - then please start writing code - that is a program, you are a programmer. It is a non-trivial thing you would be asking - as in "really hard". You might want to start out looking for an existing application that already crawls the web - they exist.

if you are asking how to process a redirect - that is pretty easy, just check the response code for a redirect code ( in the 300 range ) and do another http call based on the URL you received.

usages of utl_http in a way similar to parameterized report

Praveen Ray, March 12, 2012 - 11:32 am UTC

well well well...it may be pretty easier for you, but for a rookie like me w.r.t. (with respect to) utl_http, I really don't know how to crawl through web-pages within a pl/sql program, especially when the URL depends on an input feed.

making my Question more clear:

create table email (email varchar2(50));

insert into email values ('xyz@yahoo.com');
insert into email values ('xyz@gmail.com');

commit;

declare
-- ...
l_url varchar2(100) := ' http://asktom.oracle.com/pls/apex/f?p=100:60:0:';
cursor url_tst is select email from email;
begin
for i_url in url_tst loop
-- here i want to capture the contents of web-pages for both the emailIDs
end loop;
end;
Tom Kyte
March 12, 2012 - 12:44 pm UTC

well, in this case - you are going to have a hard time. That page is a bit different from most "normal" pages. We store the email address in the APEX session state and we POST new values up - redirecting to a GET url. In short, you won't be doing that page in an automated fashion. Not unless you were to view the source of the page, reverse engineer the entire form and figure out what we pass along. And even then - since we use APEX sessions - it won't work because you'd have to figure out how to "log onto" asktom, get a sessionid, yadda yadda yadda. Can it be done? Sure. Would it take a *significant* amount of work? Indeed it would.

What you have to do - against the site you really want to go against - is figure out HOW they decided to do it. Then, POST or GET the information they need. It'll take some detective work on your part.

And then, it'll break next week (99.99999% chance of that happening) - because the web site you are screen scraping decided to change a tiny little bit of their page.

And when you fix that, it'll break again

and again

and again


You'd best be served, as would your employer, by getting in touch with the site you are trying to scrape and

a) verify that is isn't against their terms of service
b) ask them about their API you can use to get this information in a reliable fashion.



I never said this would be easy for me, in fact I said quite the opposite. This isn't really a UTL_HTTP, plsql, Oracle database, etc question. This is a question about "how to write a web crawler".

UTL_HTTP --> FTP over SSL

Anjanava Biswas, March 31, 2012 - 12:58 pm UTC

I had to ask this, as I couldn't find it in your forum. Can we do an FTP over SSL/TLS (not SFTP or SCP) using UTL_HTTP or UTL_FTP at all (to get some plain text flat files from the FTP server)? The server I use is AIX 5.3 and it cannot do FTP over SSL with its native libraries (to an FTP server allowing only SSL/TLS connections) so the only option left is either Database stored procs (10g) or a stand alone Java stub. I made the java option work using some apache commons library, but I am inherently inclined to the DB part and hence want to know if it is possible in Oracle 10g.

A reader, June 09, 2012 - 6:11 pm UTC

>>you'd have to write a javascript virtual machine if you wanted to retrieve HTML and have the javascript run.

Is there any other workaround that I can use ?

Thanks.

Tom Kyte
June 10, 2012 - 3:36 am UTC

start researching java and see if someone has already written the code. then load that into the database and use it.

A reader, June 10, 2012 - 11:28 am UTC

Thanks Tom!

How to call Webservices From ORACLE -Forms Developers suit 10g

Abhisekh, July 29, 2013 - 7:35 am UTC

Hi Tom ,

Can you tell me how to call web-services form oracle forms-10g enverment ?

Regards
Abhisekh

UTL_HTTP

abhi, August 14, 2013 - 10:56 am UTC

hi tom can you tell stap by step utl_http implimentation as i have lack of knoledge about web integration.

AR Thangal, January 08, 2014 - 2:57 pm UTC

Dear Mr. Tom.
i have one trigger to handle after insert one table to call http request.this request has generate report newly inserted data. unfortunately the PDF has no data.
how we can handle commit changes in HTTP request.

Examble:- Tab1 has trigger name trg_tab1. after insert Tab1 copy some value to inserted in Tab2.
here inside trg_tab1,im used UTL_HTTP.REQUEST to call a report to containnig inserted tab2 data.but this report is empty.
Pls Suggest.

Tom Kyte
January 08, 2014 - 6:35 pm UTC

I would like you to think about how horrendously bad of an idea this would be - even if it "worked"!!!!!

think about it. If an insert happens - and the report is run - but then something happens that causes the transaction to fail..... so the insert is rolled back... what would have happened to the report????? you would have generated a report for data that never exists.

couple that with the fact that your insert trigger might fire once, OR IT MIGHT FIRE TWICE - for the same row.

http://asktom.oracle.com/Misc/something-different-part-i-of-iii.html
http://asktom.oracle.com/Misc/part-ii-seeing-restart.html
http://asktom.oracle.com/Misc/part-iii-why-is-restart-important-to.html
http://asktom.oracle.com/Misc/that-old-restart-problem-again.html


so, think about it - triggers that do something non-transaction should be outlawed, forbidden, made fun of (as they are wrong, if you do something non-transactional in a trigger - it is *wrong*)


so, the answer for you is:

DO NOT DO THIS IN A TRIGGER.

what you can do is schedule a job using dbms_job, that job will run the report - but it will only run the report AFTER the transaction that queued the job successfully commits. if the transaction rolls back for any reason, the job will "disappear".


And since the job runs the report AFTER the commit, the data inserted will be visible to other sessions (this is why your report does not "work" now - the data isn't committed, there is no data to be seen by other sessions/transactions).

use this approach:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:351033761220

and just submit a job like:

run_report(JOB);

in your insert trigger - and nothing more.


http://www.oracle.com/technetwork/issue-archive/2008/08-sep/o58asktom-101055.html

Passin values between form9i and web page

samer, January 28, 2014 - 7:52 pm UTC

technical environment : Forms [32 Bit] Version 9.0.2.9.0 (Production), oracle JInitiator: Version 1.3.1.9, WebUtil Version 1.0.2(Beta), window xp service pack 2 build 2600, Internet Explorer 8
i m looking how to pass values into a webpage(metar_taf.html) from a form9i builder

In detail:
the metar_taf.html web page has:
metar text item which contains airport code fox example (OLBA, LCLK, LFPO ....)and
button called submit, if it' is clicked all the metars messages for the airport having a code mentioned in the metar text_item are displayed.


i create my form called frm.fmb through which I want to modify the trigger (CALL_WEB_PAGE button) which is : WEB.SHOW_DOCUMENT (' http://aviationweather.gov/adds/metars/' ); so as it will

set metar text_item found in metar_taf.html by the airports codes mentioned above(OLBA, LCLK,...)
and

run the trigger already written on the button submit found in the metar_taf.html to generate the message html file as well.

how Form9i interact with Web pages

rida, January 29, 2014 - 10:53 am UTC

Hi Mr tom
I need your help
can you help me how I can, from oracle9i form builder passing values to a html web page and execute a trigger written on the button as well in this html web page??
any suggestion I appreciate it
Thank you

disable pdf toolbar

Dulal Sarkar, May 12, 2015 - 12:31 pm UTC

Hi Tom,
How I will disable pdf toolbar using WEB.SHOW_DOCUMENT(rep_url,'_blank')
because I want to prevent printing/save/saveas options of pdf file.
Thanks in advance.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library