码迷,mamicode.com
首页 > 数据库 > 详细

PLSQL NOTE--------network(执行权限,acl和测试)

时间:2015-12-08 22:13:36      阅读:616      评论:0      收藏:0      [点我收藏+]

标签:

1.utl_http,utl_tcp,utl_inaddr,utl_mail,utl_smtp的赋权限

grant execute on &utl_privilege to &username;

2.utl_inaddr 的使用

(1)获取本地主机名(host_name)和地址(host_address)

1 SET serveroutput on
2 BEGIN
3   DBMS_OUTPUT.PUT_LINE(UTL_INADDR.GET_HOST_NAME);  -- get local host name
4   DBMS_OUTPUT.PUT_LINE(UTL_INADDR.GET_HOST_ADDRESS);  -- get local IP addr
5 END;
6 /

(2)根据host_name 获取address或根据address取name

 1 UTL_INADDR.GET_HOST_ADDRESS ( host  IN VARCHAR2 DEFAULT NULL) 
 2 RETURN host_address VARCHAR2; 
 3 --host       The name of the host to retrieve the IP address.
 4 --host_address     The IP address of the specified host, or that of the local host if host is NULL.
 5 
 6 
 7 UTL_INADDR.GET_HOST_NAME ( ip  IN VARCHAR2 DEFAULT NULL)
 8 RETURN host_name VARCHAR2;
 9 --ip     The IP address of the host used to determine its host name. If ip is not NULL, the official name of the host with its domain name is returned. 
  --If this is NULL, the name of the local host is returned and the name does not contain the domain to which the local host belongs. 10 11 --host_name The name of the local or remote host of the specified IP address.

 3.create and drop acl

 1  declare
 2  l_acl varchar2(400) := ‘utl_mail.xml’;
 3  l_username varchar2(400) := victor;
 4  l_host varchar2(400) := *;
 5  begin
 6    dbms_network_acl_admin.create_acl (
 7      acl         =>l_acl,
 8      description => Allow mail to be send,
 9      principal   => l_username,
10      is_grant    => TRUE,
11      privilege   => connect12      );
13  
14    dbms_network_acl_admin.add_privilege (
15    acl       =>l_acl,
16    principal => l_username,
17    is_grant  => TRUE,
18    privilege => resolve19    );
20  
21    dbms_network_acl_admin.assign_acl(
22    acl  => l_acl,
23    host => l_host
24    );
25   end;
26 /
1 declare
2 l_acl varchar2(400) := utl_smtp.xml;
3 begin
4 dbms_network_acl_admin.drop_acl(
5     l_acl
6   );
7 end;
8 /

4.send email (utl_mail 或者utl_smtp)

(1)utl_mail

 DECLARE
   l_mail_to_list VARCHAR2 (4000);
   l_mail_cc_list VARCHAR2 (4000);
   crlf        VARCHAR2(2)  := chr(13)||chr(10);
 BEGIN
   l_mail_to_list  := sender.mail_address;
   l_mail_cc_list  := receiver1.mail_address,receiver2.mail_address;
   EXECUTE IMMEDIATE ALTER SESSION SET smtp_out_server = ‘‘127.0.0.1‘‘‘;
   UTL_MAIL.send(sender => l_mail_to_list,
                 recipients => l_mail_cc_list,
                 subject => test for send mail by utl_mail,
                 message => Hi All,||crlf||crlf
                               ||this is test email ,please do not reply to it!||crlf||crlf
                              ||Thanks!||crlf,
                 mime_type => text; charset=us-ascii);
 EXCEPTION
  WHEN others then
    raise_application_error(-20000, Unable to send mail);
 END;
 /
 show errors;

(2)utl_smtp

 1 declare
 2     TYPE Varchar2Array FORCE IS VARRAY (2147483647) OF VARCHAR2 (32767) ;
 3     p_from VARCHAR:= XXX@XXX;
 4     p_to VarcharArray := VarcharArray(XXX@XXX‘,XXX@XXX);
 5     p_subject VARCHAR2 := just for at&t plsql test;
 6     p_message VARCHAR2 := Date:||to_char(sysdate,yyyy-mm-dd hh24:mi:ss‘)||CRLF
 7                 ||Dear All,||CRLF
 8                 ||This is plsql test mail, please DO NOT Reply this mail.||CRLF
 9                 ||But, would you please tell me the succee on skype !||CRLF
10                 ||Thanks||CRLF
11                 ||victor;
12     l_session utl_smtp.Connection;
13     l_i INTEGER ;
14     l_msg VARCHAR2(32767);
15     CRLF VARCHAR2(2):= chr(13)||chr(10);
16     username varchar2(400) := username;
17     pass varchar2(400) := password;
18     host varchar2(200) := www.baidu.com;
19     port integer := 25;
20 BEGIN
21     l_session  := utl_smtp.Open_Connection(host,port);
22     utl_smtp.HELO(l_session,host);
23     UTL_SMTP.command(v_conn, AUTH LOGIN);            
24   UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(username))));
25   UTL_SMTP.command(v_conn,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(pass))));
26     utl_smtp.Mail(l_session, <||p_from||>);
27     l_i := 0;
28     WHILE l_i  < p_to.count
29     LOOP
30       BEGIN
31           utl_smtp.Rcpt(l_session, <||p_to(l_i+1)||>);
32       END;
33         l_i  := l_i  + 1;
34     END LOOP;
35     UTL_SMTP.OPEN_DATA(l_session);
36     l_msg := Date:||to_char(sysdate,yyyy-mm-dd hh24:mi:ss‘)||CRLF;
37     l_msg := l_msg||‘From: "sender"‘ ||‘<‘||p_from||‘>‘||CRLF;
38     for i in 1..p_to.count
39     LOOP
40         l_msg := l_msg||‘TO: "Recipient"‘||‘<‘||p_to(i)||‘>‘||CRLF;
41     END LOOP;
42     l_msg := l_msg||‘Subject: ‘ ||p_subject||CRLF;
43     l_msg := l_msg||‘Content-Type: text/plain‘;
44     UTL_SMTP.write_raw_data(l_session, utl_raw.cast_to_raw(l_msg));
45     UTL_SMTP.write_raw_data(l_session, utl_raw.cast_to_raw(CRLF||p_message));
46     UTL_SMTP.close_data(l_session);
47     UTL_SMTP.quit(l_session);
48 EXCEPTION
49   WHEN others  THEN
50   BEGIN
51     dbms_output.put_line (‘send_message error: ‘||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE );
52       dbms_output.put_line (‘send_message error: ‘||sqlerrm );
53     END;
54 END;
55 /

 (3)utl_tcp

 1 CREATE OR REPLACE PROCEDURE SEND_MAIL (
 2   msg_from    varchar2 := EMAILADDRESS@DOMAIN.COM‘,    ----- MAIL BOX SENDING THE EMAIL
 3   msg_to      varchar2 := EMAILADDRESS@DOMAIN.COM‘,    ----- MAIL BOX RECIEVING THE EMAIL
 4   msg_subject varchar2 := Output file TEST1‘,          ----- EMAIL SUBJECT
 5   msg_text    varchar2 := THIS IS THE TEXT OF THE EMAIL MESSAGE.,
 6   v_output1   varchar2 := THIS IS THE TEXT OF THE ATTACHMENT FILE. THIS TEXT WILL BE IN A TEXT FILE ATTACHED TO THE EMAIL.)
 7 IS
 8   c  utl_tcp.connection;
 9   rc integer;
10   crlf VARCHAR2(2):= CHR(13)||CHR(10);
11   mesg VARCHAR2(32767);
12 BEGIN
13   c := utl_tcp.open_connection(196.35.140.18‘, 25);       ----- OPEN SMTP PORT CONNECTION
14   rc := utl_tcp.write_line(c, HELO 196.35.140.18‘);       ----- PERFORMS HANDSHAKING WITH SMTP SERVER
15   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
16   rc := utl_tcp.write_line(c, EHLO 196.35.140.18‘);       ----- PERFORMS HANDSHAKING, INCLUDING EXTRA INFORMATION
17   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
18   rc := utl_tcp.write_line(c, MAIL FROM: ||msg_from);    ----- MAIL BOX SENDING THE EMAIL
19   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
20   rc := utl_tcp.write_line(c, RCPT TO: ||msg_to);        ----- MAIL BOX RECIEVING THE EMAIL
21   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
22   rc := utl_tcp.write_line(c, DATA‘);                     ----- EMAIL MESSAGE BODY START
23   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
24   rc := utl_tcp.write_line(c, Date: ||TO_CHAR( SYSDATE, dd Mon yy hh24:mi:ss ));
25   rc := utl_tcp.write_line(c, From: ||msg_from|| <||msg_from||>);
26   rc := utl_tcp.write_line(c, MIME-Version: 1.0);
27   rc := utl_tcp.write_line(c, To: ||msg_to|| <||msg_to||>);
28   rc := utl_tcp.write_line(c, Subject: ||msg_subject);
29   rc := utl_tcp.write_line(c, Content-Type: multipart/mixed;‘);  ----- INDICATES THAT THE BODY CONSISTS OF MORE THAN ONE PART
30   rc := utl_tcp.write_line(c,  boundary="-----SECBOUND"‘);       ----- SEPERATOR USED TO SEPERATE THE BODY PARTS
31   rc := utl_tcp.write_line(c, );                                ----- DO NOT REMOVE THIS BLANK LINE - PART OF MIME STANDARD
32   rc := utl_tcp.write_line(c, -------SECBOUND‘);
33   rc := utl_tcp.write_line(c, ‘Content-Type: text/plain‘);        ----- 1ST BODY PART. EMAIL TEXT MESSAGE
34   rc := utl_tcp.write_line(c, ‘Content-Transfer-Encoding: 7bit‘);
35   rc := utl_tcp.write_line(c, );
36   rc := utl_tcp.write_line(c, msg_text);                          ----- TEXT OF EMAIL MESSAGE
37   rc := utl_tcp.write_line(c, );
38   rc := utl_tcp.write_line(c, ‘-------SECBOUND‘);
39   rc := utl_tcp.write_line(c, ‘Content-Type: text/plain;‘);       ----- 2ND BODY PART.
40   rc := utl_tcp.write_line(c, ‘ name="Test.txt"‘);
41   rc := utl_tcp.write_line(c, ‘Content-Transfer_Encoding: 8bit‘);
42   rc := utl_tcp.write_line(c, ‘Content-Disposition: attachment;‘); ----- INDICATES THAT THIS IS AN ATTACHMENT
43   rc := utl_tcp.write_line(c, ‘ filename="Test.txt"‘);             ----- SUGGESTED FILE NAME FOR ATTACHMENT
44   rc := utl_tcp.write_line(c, );
45   rc := utl_tcp.write_line(c, v_output1);
46   rc := utl_tcp.write_line(c, ‘-------SECBOUND--‘);
47   rc := utl_tcp.write_line(c, );
48   rc := utl_tcp.write_line(c, ‘.‘);                    ----- EMAIL MESSAGE BODY END
49   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
50   rc := utl_tcp.write_line(c, ‘QUIT‘);                 ----- ENDS EMAIL TRANSACTION
51   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
52   utl_tcp.close_connection(c);                         ----- CLOSE SMTP PORT CONNECTION
53 END;
54 /

 4.utl_http

 1 create or replace function sendHttpPost
 2 (p_url in varchar2, request_clob in CLOB, timeout integer := 1000 ) 
 3 return CLOB is
 4     l_http_request   UTL_HTTP.req;
 5     l_http_response  UTL_HTTP.resp;
 6     l_buffer_size constant integer := 512;
 7     l_lines_count constant integer := 20;
 8     l_line_size constant integer :=  50;
 9     l_clob_response clob := empty_clob();
10     l_line           VARCHAR2(128);
11     l_substring_msg  VARCHAR2(512);
12     l_raw_data       RAW(512);
13     function get_host(url in varchar2) return varchar2 is 
14         doubleslash integer;
15         endidx integer;
16         portidx integer;
17         l_host_name varchar2(32767);
18         l_port varchar2(32767);
19     begin
20         doubleslash := instr(url, ‘//‘, 1, 1) ;
21         doubleslash := case doubleslash when 0 then 1 else doubleslash+2 end;
22         endidx := instr(url, ‘/‘, doubleslash, 1);
23         endidx := case endidx when 0 then length(url)+1 else endidx end;
24         l_host_name := substr(url, doubleslash, endidx-doubleslash);
25         return l_host_name;
26     end;   
27 begin
28     DBMS_OUTPUT.put_line(‘*********************send http request start‘);
29     UTL_HTTP.SET_TRANSFER_TIMEOUT(timeout);
30     DBMS_OUTPUT.put_line(‘*********************send request‘);
31     l_http_request := UTL_HTTP.begin_request(url => p_url, method => ‘POST‘, http_version => ‘HTTP/1.1‘);
32     UTL_HTTP.set_header(l_http_request, ‘User-Agent‘, ‘Mozilla/4.0‘);
33     UTL_HTTP.set_header(l_http_request, ‘Host‘, get_host(p_url));
34     UTL_HTTP.set_header(l_http_request, ‘Connection‘, ‘close‘);
35     UTL_HTTP.set_header(l_http_request, ‘Content-Type‘, ‘text/xml;charset=UTF-8‘);
36     UTL_HTTP.set_header(l_http_request, ‘SOAPAction‘, ‘"GetContractInvSmryReqst"‘);
37     UTL_HTTP.set_header(l_http_request, ‘Content-Length‘, LENGTH(request_clob));
38 
39      <<request_loop>>
40     FOR i IN 0..CEIL(LENGTH(request_clob) / l_buffer_size) - 1 LOOP
41         l_substring_msg := SUBSTR(request_clob, i * l_buffer_size + 1, l_buffer_size);
42         BEGIN
43             l_raw_data := utl_raw.cast_to_raw(l_substring_msg);
44             UTL_HTTP.write_raw(r => l_http_request, data => l_raw_data);
45             EXCEPTION
46                 WHEN NO_DATA_FOUND THEN
47                     EXIT request_loop;
48         END;
49     END LOOP request_loop;
50     l_http_response := UTL_HTTP.get_response(l_http_request);
51     DBMS_OUTPUT.put_line(‘Response> http_version: "‘ ||l_http_response.http_version || ‘"‘);
52     DBMS_OUTPUT.put_line(‘Response> Status: ‘ || l_http_response.Status_code);
53     DBMS_OUTPUT.put_line(‘Response> Reason Phrase: ‘ || l_http_response.reason_phrase);
54     BEGIN
55         <<response_loop>>
56         LOOP
57             UTL_HTTP.read_raw(l_http_response, l_raw_data, l_buffer_size);
58             l_clob_response := l_clob_response || UTL_RAW.cast_to_varchar2(l_raw_data);
59         END LOOP response_loop;
60         EXCEPTION
61             WHEN UTL_HTTP.end_of_body THEN
62                 UTL_HTTP.end_response(l_http_response);
63         END;
64     DBMS_OUTPUT.put_line(‘Response> length: "‘ || LENGTH(l_clob_response) || ‘"‘);
65     DBMS_OUTPUT.put_line(CHR(10) || ‘=== Print first ‘ || l_lines_count || ‘ lines of HTTP response... ===‘ || CHR(10) || CHR(10));
66  
67 <<print_response>>
68  FOR i IN 0..CEIL(LENGTH(l_clob_response) / l_line_size) - 1 LOOP
69        l_line := SUBSTR(l_clob_response, i * l_line_size + 1, l_line_size);
70        DBMS_OUTPUT.put_line(‘[‘ || LPAD(i, 2, ‘0‘) || ‘]: ‘ || l_line);
71         EXIT WHEN i > l_lines_count - 1;
72     END LOOP print_response;
73  IF l_http_request.private_hndl IS NOT NULL THEN
74         UTL_HTTP.end_request(l_http_request);
75     END IF;
76     IF l_http_response.private_hndl IS NOT NULL THEN
77         UTL_HTTP.end_response(l_http_response);
78     END IF;
79     DBMS_OUTPUT.put_line(‘************************ start print response‘);
80     printXML(l_clob_response);
81     return l_clob_response ;
82 end;
83 /
84 show errors;

 

PLSQL NOTE--------network(执行权限,acl和测试)

标签:

原文地址:http://www.cnblogs.com/ct-blog/p/5031087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!