---
title: "ORACLE WEBService调用"
author: "wjm901215"
date: "2013-06-03T10:54:31Z"
tags: []
categories: ["数据库"]
slug: "oracle-webservice"
draft: false
lastmod: "2013-06-03T10:54:31Z"
id: 1881444
url: "https://wangjunma.iteye.com/blog/1881444"
---

```
create or replace function soap_call
  ( p_req_body       in varchar2
  , p_target_url in varchar2
  , p_soap_action in varchar2 default 'none'
  ) return xmltype
  is
    l_soap_request  varchar2(30000);
    l_soap_response varchar2(30000);
    http_req utl_http.req;
    http_resp utl_http.resp;
  begin
    l_soap_request := '<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>'|| p_req_body ||'</S:Body></S:Envelope>';
    http_req:= utl_http.begin_request
               ( p_target_url
               , 'POST'
               , 'HTTP/1.1'
               );
    utl_http.set_header(http_req, 'Content-Type', 'text/xml; charset=UTF8');
    utl_http.set_header(http_req, 'Content-Length', length(l_soap_request));
    utl_http.set_header(http_req, 'SOAPAction', p_soap_action);
    utl_http.write_text(http_req, l_soap_request);
    -- the actual call to the service is made here
    http_resp:= utl_http.get_response(http_req);
    utl_http.read_text(http_resp, l_soap_response);
    utl_http.end_response(http_resp);

    -- only return from the soap response - that is: the content of the body element in the SOAP envelope
    return XMLType.createXML(l_soap_response).extract('/S:Envelope/S:Body/child::node()'
                     , 'xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"');
  end;

--webservice调用代码
CREATE OR REPLACE FUNCTION SEND_INDUCED_MESSAGE
(
    p_SIMNUM              IN VARCHAR2
  , p_HEIGHT              IN VARCHAR2 DEFAULT '32'
  , p_WIDTH               IN VARCHAR2 DEFAULT '0'
  , p_COLOR               IN VARCHAR2 DEFAULT '1'
  , p_PARKNUM             IN VARCHAR2
) RETURN VARCHAR2 AS
  l_response_call          XMLType;
  l_request_body           varchar2(20000);
  l_target_namespace       varchar2(200);
  l_target_url             varchar2(200);
BEGIN
  l_target_namespace  := 'http://led.innotek.com/';
  l_target_url        := 'http://192.168.10.100:8080/WebApplication/LedDisplayServiceService';
  l_request_body      :=
       '<ns:sendtext xmlns:ns="'||l_target_namespace||'">
            <simNum>'||p_SIMNUM||'</simNum>
            <tHeight>'||p_HEIGHT||'</tHeight>
            <tWidth>'||p_WIDTH||'</tWidth>
            <color>'||p_COLOR||'</color>
            <parkNum>'||p_PARKNUM||'</parkNum>
        </ns:sendtext>';
  l_response_call := soap_call(l_request_body, l_target_url, 'sendtext');
  return l_response_call.extract('//return/text()').getStringVal();
END ;
```

--创建控制列表并赋权 begin dbms\_network\_acl\_admin.create\_acl( acl =&gt; 'utl\_http.xml', description =&gt; 'Http Access', principal =&gt; 'SMARTFEE', is\_grant =&gt; TRUE, privilege =&gt; 'connect', start\_date =&gt; null, end\_date =&gt; null ); --添加新用户  
dbms\_network\_acl\_admin.add\_privilege ( acl =&gt; 'utl\_http.xml', principal =&gt; 'SMARTFEE', is\_grant =&gt; TRUE, privilege =&gt; 'resolve', start\_date =&gt; null, end\_date =&gt; null ); --给网络分配一个访问控制列表  
dbms\_network\_acl\_admin.assign\_acl ( acl =&gt; 'utl\_http.xml', host =&gt; 'sms.pf.innotek.com', lower\_port =&gt; 8880, upper\_port =&gt; 8880 ); commit; end; /

```
--访问控制列表视图


select * from DBA_NETWORK_ACLS;


select * from DBA_NETWORK_ACL_PRIVILEGES;


select * from USER_NETWORK_ACL_PRIVILEGES;



SELECT *
FROM TABLE(DBMS_NETWORK_ACL_UTILITY.domains('www.chinastock.com.cn'));






--测试


DECLARE
l_url VARCHAR2(300) := 'http://www.test.com.cn/stock.xml';


l_http_request UTL_HTTP.req;


l_http_response UTL_HTTP.resp;


BEGIN
-- Make a HTTP request and get the response.


l_http_request := UTL_HTTP.begin_request(l_url);


l_http_response := UTL_HTTP.get_response(l_http_request);


UTL_HTTP.end_response(l_http_response);
```

END;