從PL/SQL調用web services實例

web services是目前比較流行的系統之間信息交互的一種方式,近段研究了下通過Oracle數據庫utl_http包調用web services,以下爲例子,基於Oracle 11gr2測試。

此兩種方式基本相似,只是在寫法、調用上有些出入。涉及的web services服務器端代碼較簡單,採用axis2(v1.5.4)實現,可直接利用axis官方提供的例子。

附:web services源碼

方式一:

  procedure testWebServices is
    soap_request varchar2(30000);
    soap_respond varchar2(30000);
    http_req     utl_http.req;
    http_resp    utl_http.resp;
    l_ws_url varchar2(1000);
    l_ws_function varchar2(100);
    l_ws_soapaction varchar2(100);
  begin
    --不傳遞參數
    soap_request := '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.apache.org/axis2">
    <soap:Body>
    </soap:Body>
    </soap:Envelope>';
    l_ws_url := 'http://IP:port/axis2/services/Version/';
    l_ws_soapaction := 'urn:getVersion';
    --傳遞參數
    soap_request := '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.apache.org/axis2">
    <soap:Body>
    <tns:HelloWorld>
    <tns:name>中國</tns:name>
    </tns:HelloWorld>    
    </soap:Body>
    </soap:Envelope>';
    l_ws_url := 'http://ip:port/axis2/services/HelloWorldService/';
    l_ws_soapaction := 'urn:sayHello';
    
    http_req := utl_http.begin_request(l_ws_url,
                                       'POST',
                                       utl_http.http_version_1_1);
    utl_http.set_header(http_req,
                        'Content-Type',
                        'text/xml; charset=utf-8'
                        --'application/soap+xml; charset=utf-8'
                        );
    utl_http.set_header(http_req, 'Content-Length', length(soap_request));
    utl_http.set_header(http_req, 'SOAPAction', l_ws_soapaction);
    utl_http.write_text(http_req, soap_request);
    http_resp := utl_http.get_response(http_req);
    utl_http.read_text(http_resp, soap_respond);
    utl_http.end_response(http_resp);
    dbms_output.put_line('soap_respond = ' || soap_respond);
  exception
    WHEN OTHERS THEN
      dbms_output.put_line('調用異常!!!!!!!!');
  end;

方式二:

  procedure testWebServices2 is
    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_string_request  VARCHAR2(512);
    l_line            VARCHAR2(128);
    l_substring_msg   VARCHAR2(512);
    l_raw_data        RAW(512);
    l_clob_response   CLOB;
    l_host_name       VARCHAR2(128) := 'xx.xx.xx.xx';
    l_port            VARCHAR2(128) := '8080';
    l_zip             VARCHAR2(128) := '78909中國';
    l_resp_xml        XMLType;
    l_result_XML_node VARCHAR2(128);
    l_NAMESPACE_SOAP  VARCHAR2(128) := 'xmlns="http://www.w3.org/2003/05/soap-envelope"';
    l_response_city   VARCHAR2(128);
    l_response_date   VARCHAR2(128);
    l_response_temp   VARCHAR2(128);
    
    l_ws_url varchar2(1000);
    soap_respond varchar2(30000);
    
  begin
    l_string_request := '<?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
     <soap12:Body>
      <GetCityForecastByZIP xmlns="http://ws.apache.org/axis2">
       <name>' || l_zip || '</name>
      </GetCityForecastByZIP>
     </soap12:Body>
    </soap12:Envelope>';
    UTL_HTTP.set_transfer_timeout(60);
    
    l_ws_url := 'http://IP:port/axis2/services/HelloWorldService/sayHello';
    
    l_http_request := UTL_HTTP.begin_request(url => l_ws_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, 'Connection', 'close');
    UTL_HTTP.set_header(l_http_request, 'Content-Type', 'application/soap+xml; charset=utf-8');
    UTL_HTTP.set_header(l_http_request, 'Content-Length', LENGTH(l_string_request));

    <<request_loop>>
    FOR i IN 0..CEIL(LENGTH(l_string_request) / l_buffer_size) - 1 LOOP
        l_substring_msg := SUBSTR(l_string_request, 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> status_code: "' || l_http_response.status_code || '"');
    DBMS_OUTPUT.put_line('Response> reason_phrase: "' ||l_http_response.reason_phrase || '"');
    DBMS_OUTPUT.put_line('Response> http_version: "' ||l_http_response.http_version || '"');   

    --utl_http.read_text(l_http_response, soap_respond);
    --dbms_output.put_line('soap_respond = ' || soap_respond);
    
    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 result ===' || CHR(10) || CHR(10));
    
    DBMS_OUTPUT.put_line ( 'Result> l_response_city=' || l_response_city);
    DBMS_OUTPUT.put_line ( 'Result> l_response_date=' || l_response_date);
    DBMS_OUTPUT.put_line ( 'Result> l_response_temp=' || l_response_temp);

    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;
    
  exception
    WHEN OTHERS THEN
      dbms_output.put_line('調用異常!!!!!!!!');
  end;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章