【问题标题】:How to run http web service request from pl/sql package如何从 pl/sql 包运行 http web 服务请求
【发布时间】:2014-09-08 14:27:12
【问题描述】:

我开发了一个新的程序来调用 Web 服务,但是当我运行它时它显示 Oracle 适配器错误。您能否指导我如何解决此错误?代码下面有问题吗?

/* Formatted on 17/07/2014 16:49:02 (QP5 v5.185.11230.41888) */
CREATE OR REPLACE PROCEDURE APPS.xx_web_id
IS
l_http_request    UTL_HTTP.req;
l_http_response   UTL_HTTP.resp;
l_response_text   VARCHAR2 (10000);
BEGIN
-- preparing request
l_http_request :=
  UTL_HTTP.begin_request ('https://freegeoip.net/xml/82.39.109.147',
                          'POST',
                          'HTTP/1.1');

l_http_response := UTL_HTTP.get_response (l_http_request);

UTL_HTTP.read_text (l_http_response, l_response_text);

DBMS_OUTPUT.put_line (l_response_text);

UTL_HTTP.end_response (l_http_response);
EXCEPTION
WHEN UTL_HTTP.end_of_body
THEN
  UTL_HTTP.end_response (l_http_response);
END;

【问题讨论】:

  • 如果您使用的是 Oracle 11g 或更高版本,您可能会因为缺少 ACL(访问控制列表)而受阻。它根据 DBA 或管理员定义的例外情况授予/拒绝从数据库服务器传出和传入的网络流量。它是通过运行带有标识用户帐户、源和目标端口/IP 地址范围等参数的过程调用来实现的。如果您尝试通过 Internet 发送(或接收)请求,请仔细考虑。阅读有关 ACL 分配权限的安全建议。
  • 并且错误的详细信息是?
  • 请提供错误详情。您可能必须提供正确的标题。检查我的帖子中的答案。几天后,我得到了以下答案。 stackoverflow.com/questions/24838138/…

标签: oracle web service plsql package


【解决方案1】:

首先你应该配置ACL;

BEGIN
dbms_network_acl_admin.create_acl(acl => 'pkg.xml',
                                description => 'Normal Access',
                                principal => 'CONNECT',
                                is_grant => TRUE,
                                privilege => 'connect',
                                start_date => NULL,
                                end_date => NULL);
    END;
    /

    BEGIN
    dbms_network_acl_admin.add_privilege(acl => 'utlpkg.xml',
                                   principal => 'XXXX', --- user name
                                   is_grant => TRUE,
                                   privilege => 'connect',
                                   start_date => NULL,
                                   end_date => NULL);
      END;
      /


   BEGIN
      dbms_network_acl_admin.assign_acl(acl => 'pkg.xml',
                                host => 'HOST ADDRESS',--  Web Server URL
                                lower_port => 7222, -- Web  Server PORTs
                                upper_port => 7222);
    END;
   /

然后您应该创建将称为 Web 服务的过程。我使用的示例您可以在下面找到;

CREATE OR REPLACE PROCEDURE web_service_call()
AS

l_http_request     UTL_HTTP.req;
l_http_response    UTL_HTTP.resp;
l_buffer_size      NUMBER (10)      := 512;
l_line_size        NUMBER (10)      := 50;
l_lines_count      NUMBER (10)      := 20;
l_clob_request   CLOB;
l_line             VARCHAR2 (32767);
l_substring_msg    VARCHAR2 (32767);
l_raw_data         RAW (512);
l_clob_response    CLOB;
l_host_name        VARCHAR2 (128)   := 'get.bla.bla';
l_port             VARCHAR2 (128)   := '7222'; 
req_length binary_integer;
bufferY   varchar(2000);
amount  pls_integer:=2000;
offset  pls_integer:=1;

BEGIN

  l_clob_request := 'xml'; 
  l_http_request :=
     UTL_HTTP.begin_request (url               =>    'http://'
                                                  || l_host_name
                                                  || ':'
                                                  || l_port
                                                  || '/Something/GetSomething',
                             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',
                       l_host_name || ':' || l_port
                      );
  UTL_HTTP.set_header (l_http_request, 'Connection', 'close');
  utl_http.set_header(l_http_request, 'Content-Type', 'text/xml;charset=ISO-8859-9'); 
  UTL_HTTP.set_header (l_http_request, 'SOAPAction', '"sayHello"');
  UTL_HTTP.set_header (l_http_request,
                       'Content-Length',
                       LENGTH (l_clob_request)
                      );   

        req_length := DBMS_LOB.getlength (l_clob_request);
        offset :=1;
        bufferY :=null;
        amount:=2000;

  WHILE (offset < req_length)
   LOOP
      DBMS_LOB.read (l_clob_request,
                     amount,
                     offset,
                     bufferY);
      UTL_HTTP.write_text (l_http_request, bufferY);
      offset := offset + amount;

   END LOOP;  
      l_http_response := UTL_HTTP.get_response (l_http_request); 
      l_clob_response :=null;
      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; 

  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; 
END;
 /

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2013-04-28
    • 1970-01-01
    • 2012-05-06
    • 2013-01-27
    相关资源
    最近更新 更多