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

PLSQL NOTE--------network utl_http

时间:2016-05-16 17:20:14      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

utl_http

 create or replace function sendHttpPost
 (p_url in varchar2, request_clob in CLOB, timeout integer := 1000 ) 
 return CLOB is
     l_http_request   UTL_HTTP.req;
     l_http_response  UTL_HTTP.resp;
     l_buffer_size constant integer := 512;
     l_lines_count constant integer := 20;
     l_line_size constant integer :=  50;
     l_clob_response clob := empty_clob();
     l_line           VARCHAR2(128);
     l_substring_msg  VARCHAR2(512);
     l_raw_data       RAW(512);
     function get_host(url in varchar2) return varchar2 is 
         doubleslash integer;
         endidx integer;
         portidx integer;
         l_host_name varchar2(32767);
         l_port varchar2(32767);
     begin
         doubleslash := instr(url, //, 1, 1) ;
         doubleslash := case doubleslash when 0 then 1 else doubleslash+2 end;
         endidx := instr(url, /, doubleslash, 1);
         endidx := case endidx when 0 then length(url)+1 else endidx end;
         l_host_name := substr(url, doubleslash, endidx-doubleslash);
         return l_host_name;
     end;   
 begin
     DBMS_OUTPUT.put_line(*********************send http request start);
     UTL_HTTP.SET_TRANSFER_TIMEOUT(timeout);
     DBMS_OUTPUT.put_line(*********************send request);
     l_http_request := UTL_HTTP.begin_request(url => p_url, method => POST, http_version => HTTP/1.1);
     UTL_HTTP.set_header(l_http_request, User-Agent, Mozilla/4.0);
     UTL_HTTP.set_header(l_http_request, Host, get_host(p_url));
     UTL_HTTP.set_header(l_http_request, Connection, close);
     UTL_HTTP.set_header(l_http_request, Content-Type, text/xml;charset=UTF-8);
     UTL_HTTP.set_header(l_http_request, SOAPAction, "GetContractInvSmryReqst");
     UTL_HTTP.set_header(l_http_request, Content-Length, LENGTH(request_clob));
 
      <<request_loop>>
     FOR i IN 0..CEIL(LENGTH(request_clob) / l_buffer_size) - 1 LOOP
         l_substring_msg := SUBSTR(request_clob, i * l_buffer_size + 1, l_buffer_size);
         BEGIN
             l_raw_data := utl_raw.cast_to_raw(l_substring_msg);
             UTL_HTTP.write_raw(r => l_http_request, data => l_raw_data);
             EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                     EXIT request_loop;
         END;
     END LOOP request_loop;
     l_http_response := UTL_HTTP.get_response(l_http_request);
     DBMS_OUTPUT.put_line(Response> http_version: " ||l_http_response.http_version || ");
     DBMS_OUTPUT.put_line(Response> Status:  || l_http_response.Status_code);
     DBMS_OUTPUT.put_line(Response> Reason Phrase:  || l_http_response.reason_phrase);
     BEGIN
         <<response_loop>>
         LOOP
             UTL_HTTP.read_raw(l_http_response, l_raw_data, l_buffer_size);
             l_clob_response := l_clob_response || UTL_RAW.cast_to_varchar2(l_raw_data);
         END LOOP response_loop;
         EXCEPTION
             WHEN UTL_HTTP.end_of_body THEN
                 UTL_HTTP.end_response(l_http_response);
         END;
     DBMS_OUTPUT.put_line(Response> length: " || LENGTH(l_clob_response) || ");
     DBMS_OUTPUT.put_line(CHR(10) || === Print first  || l_lines_count ||  lines of HTTP response... === || CHR(10) || CHR(10));
  
 <<print_response>>
  FOR i IN 0..CEIL(LENGTH(l_clob_response) / l_line_size) - 1 LOOP
        l_line := SUBSTR(l_clob_response, i * l_line_size + 1, l_line_size);
        DBMS_OUTPUT.put_line([ || LPAD(i, 2, 0) || ]:  || l_line);
         EXIT WHEN i > l_lines_count - 1;
     END LOOP print_response;
  IF l_http_request.private_hndl IS NOT NULL THEN
         UTL_HTTP.end_request(l_http_request);
     END IF;
     IF l_http_response.private_hndl IS NOT NULL THEN
         UTL_HTTP.end_response(l_http_response);
     END IF;
     DBMS_OUTPUT.put_line(************************ start print response);
     printXML(l_clob_response);
     return l_clob_response ;
 end;
 /
 show errors;

 

PLSQL NOTE--------network utl_http

标签:

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

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