【问题标题】:invoke a webservice through pl/sql block通过 pl/sql 块调用 web 服务
【发布时间】:2010-10-25 22:08:41
【问题描述】:

如何通过我们知道 url、用户名和密码的 pl/sql 块调用 web 服务。

以及如何查看响应?

给出一些示例代码...

提前致谢

我使用了以下代码:

CREATE OR REPLACE FUNCTION READ_DATA_FROM_WS (url IN VARCHAR2,
                                              username IN VARCHAR2,
                                              password IN VARCHAR2)
   RETURN CLOB
IS
   req           UTL_HTTP.req;
   resp          UTL_HTTP.resp;
   DATA          VARCHAR2 (2000);
   data1         CLOB;
   def_timeout   PLS_INTEGER;
   l_envelope       VARCHAR2(32767);
BEGIN
req := utl_http.begin_request(url, 'POST','HTTP/1.0');

UTL_HTTP.set_authentication (req, username, password);
utl_http.set_header(req, 'Content-Type', 'text/xml'); 

resp := utl_http.get_response(req);

   IF (resp.status_code = UTL_HTTP.http_ok)
   THEN
      UTL_HTTP.set_body_charset (resp, 'UTF-8');    
  BEGIN
         LOOP
            UTL_HTTP.read_text (resp, DATA);
            data1 := data1 || DATA;           
         END LOOP;
      EXCEPTION
         WHEN UTL_HTTP.end_of_body
         THEN     
            UTL_HTTP.end_response (resp);
            UTL_HTTP.set_transfer_timeout (def_timeout);
         WHEN OTHERS
         THEN
            NULL;
      END;
      UTL_HTTP.set_transfer_timeout (def_timeout);
   ELSE
      UTL_HTTP.end_response (resp);
      DBMS_OUTPUT.put_line ('HTTP response status code: ' || resp.status_code);
   END IF;
   RETURN (data1);
END read_data_from_ws;
/

【问题讨论】:

    标签: sql web-services plsql invoke


    【解决方案1】:

    我使用 pl/sql 的 web 服务没有问题! 我正在使用这个(+我自己的改进):http://www.oracle-base.com/dba/miscellaneous/soap_api.sql

    确保您正确定义了名称空间,我认为您应该只使用它来检索 ASCII 而不是二进制数据...

    【讨论】:

    • 其他 cmets 根本没有回答这个问题!只是说一些他们不了解 Web 服务和 SOAP 消息的概念。给我-1的你,应该先调查清楚问题再三思。
    • 我不知道它是怎么变成-1的……我是新手……可能是我误点击了一些地方……道歉:(
    • 别抱歉!我不认为是你,因为你没有足够的分数来做那个动作。
    【解决方案2】:

    这里是一些示例代码。遗漏了一些部分,但它给了你一个想法。该函数返回 WMS Web 服务的功能 XML。

       function getcapabilities(p_url     varchar2
                               ,p_version varchar2) return xmltype is
          pragma autonomous_transaction;
    
          req                utl_http.req;
          resp               utl_http.resp;
          c                  varchar2(255);
          l_clob             clob;
       begin
          dbms_lob.createtemporary(lob_loc => l_clob, cache => true, dur => dbms_lob.call);
          -- -----------------------------------
          -- OPEN TEMPORARY LOB FOR READ / WRITE
          -- -----------------------------------
          dbms_lob.open(lob_loc => l_clob, open_mode => dbms_lob.lob_readwrite);
    
          utl_http.set_proxy(proxy => <proxy>, no_proxy_domains => <no_proxy>);
    
          /* 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);
    
          if instr(p_url, '?') > 0
          then
             req := utl_http.begin_request(p_url || '&REQUEST=GetCapabilities&SERVICE=WMS&VERSION=' ||
                                           p_version);
          else
             req := utl_http.begin_request(p_url || '?REQUEST=GetCapabilities&SERVICE=WMS&VERSION=' ||
                                           p_version);
          end if;
    
          utl_http.set_header(req, 'User-Agent', 'Mozilla/4.0');
          resp := utl_http.get_response(req);
    
          begin
             loop
                utl_http.read_text(r => resp, data => c);
    
                /* function that adds a string to a clob */
                add_to_clob(l_clob, c);
    
             end loop;
          exception
             when utl_http.end_of_body then
                null;
             when others then
                raise;
          end;
    
          utl_http.end_response(resp);
    
          dbms_lob.close(lob_loc => l_clob);
    
          /* this was for some Oracle bug */
          execute immediate 'alter session set events =''31156 trace name context forever, level 2''';
          commit;
          return xmltype.createxml(l_clob);
       end;
    

    【讨论】:

      【解决方案3】:

      即使有办法做到这一点,这也是一个非常糟糕的做法!

      另外,这里有很多问题。这项服务将返回什么?您将如何将结果解析为 sql 可以理解的内容?您将如何处理从服务返回的错误?

      只需返回您需要返回到应用程序并让应用程序调用 Web 服务的任何内容。

      【讨论】:

      • 为什么这是不好的做法?如果 PL/SQL 是编写整个系统的主要语言,那么这不是一个坏习惯。请解释你的理由。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多