欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Oracle通過procedure調(diào)用webservice接口的全過程

 更新時(shí)間:2024年07月10日 10:52:35   作者:Roki Zhang  
存儲(chǔ)過程是一組為了完成特定功能的sql語句集合,經(jīng)過編譯后存儲(chǔ)在數(shù)據(jù)庫中,用戶通過制定存儲(chǔ)過程的名字并給出參數(shù)(如果該過程帶有參數(shù))來執(zhí)行他,本文介紹了Oracle通過procedure調(diào)用webservice接口的全過程,需要的朋友可以參考下

準(zhǔn)備工作

定義測試的webservice及其中的方法

在這里插入圖片描述

如何發(fā)布全新的webservice并測試,可以參考博客C# webservice 接口編寫、發(fā)布與測試

方法體如下

[WebMethod]
public string testProcedure(string sInput)
{
	return "執(zhí)行時(shí)間:" + DateTime.Now.ToString() + sInput;
}

Oracle語句詳情

-- 聲明變量和數(shù)據(jù)類型
declare
    req        utl_http.req;         -- HTTP請(qǐng)求句柄
    resp       utl_http.resp;        -- HTTP響應(yīng)句柄
    url        varchar2(4000) := 'http://10.xx.xx.xx:8085/WebService.asmx'; -- Web Service的URL地址
    soap_env   varchar2(4000);       -- SOAP請(qǐng)求包體
    buffer     varchar2(32767);      -- 字符串緩沖區(qū)
    utf8_data  clob;                 -- 存儲(chǔ)UTF-8編碼的CLOB數(shù)據(jù)類型(存儲(chǔ)大量文本數(shù)據(jù)的數(shù)據(jù)類型)
    raw_data   raw(32767);           -- RAW數(shù)據(jù)類型,用于存儲(chǔ)二進(jìn)制數(shù)據(jù)
    raw_buffer raw(32767);           -- RAW類型的緩沖區(qū)
    line       varchar2(32767);      -- 每行讀取的數(shù)據(jù)
    idx        integer := 1;         -- 循環(huán)索引變量
begin

    -- 構(gòu)造SOAP請(qǐng)求包體
    soap_env := 
        '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">' ||
        '<soap:Header/>' ||            -- SOAP頭,這里為空
        '<soap:Body>' ||               -- SOAP主體開始
        '  <tem:testProcedure>' ||     -- 調(diào)用Web Service的方法名
        '    <tem:sInput>' ||          -- 方法參數(shù)開始
        ' testConnect + 中文test' ||   -- 參數(shù)值,包括英文和中文字符
        '</tem:sInput>' ||             -- 方法參數(shù)結(jié)束
        '  </tem:testProcedure>' ||    -- 方法調(diào)用結(jié)束
        '</soap:Body>' ||              -- SOAP主體結(jié)束
        '</soap:Envelope>';            -- SOAP包體結(jié)束

    -- 開始發(fā)起HTTP POST請(qǐng)求,如果此部分存在疑問,請(qǐng)自行尋找前端請(qǐng)求報(bào)文相關(guān)內(nèi)容學(xué)習(xí)。
    req := utl_http.begin_request(url, 'POST', 'HTTP/1.1'); -- 初始化HTTP請(qǐng)求
    utl_http.set_header(req, 'Content-Type', 'application/soap+xml; charset=utf-8'); -- 設(shè)置Content-Type頭部
    utl_http.set_header(req, 'Content-Length', length(soap_env)); -- 設(shè)置Content-Length頭部
    utl_http.write_text(req, soap_env); -- 寫入SOAP請(qǐng)求包體到HTTP請(qǐng)求

    -- 獲取HTTP響應(yīng)
    resp := utl_http.get_response(req); -- 獲取HTTP響應(yīng)句柄
    dbms_lob.createtemporary(utf8_data, true); -- 創(chuàng)建臨時(shí)CLOB變量用于存儲(chǔ)響應(yīng)數(shù)據(jù)

    -- 循環(huán)讀取HTTP響應(yīng)體
    loop
        begin
            utl_http.read_raw(resp, raw_buffer, 32767); -- 讀取響應(yīng)體到RAW緩沖區(qū)
            dbms_lob.writeappend(utf8_data,                   -- 將RAW緩沖區(qū)的數(shù)據(jù)追加到CLOB中
                                 utl_raw.length(raw_buffer), -- 數(shù)據(jù)長度
                                 utl_raw.cast_to_varchar2(raw_buffer)); -- 將RAW轉(zhuǎn)換為VARCHAR2再寫入CLOB
        exception
            when utl_http.end_of_body then
                exit; -- 如果到達(dá)響應(yīng)體結(jié)尾,則退出循環(huán)
            when others then
                dbms_output.put_line('在讀取響應(yīng)時(shí)發(fā)生錯(cuò)誤: ' || sqlerrm); -- 其他異常處理
                exit; -- 退出循環(huán)
        end;
    end loop;

    utl_http.end_response(resp); -- 結(jié)束HTTP響應(yīng)

    -- 循環(huán)讀取并打印CLOB中的內(nèi)容
    idx := 1;
    while idx <= dbms_lob.getlength(utf8_data) loop
        line := dbms_lob.substr(utf8_data, 255, idx); -- 從CLOB中讀取一行數(shù)據(jù)
        dbms_output.put_line(line); -- 打印讀取的一行數(shù)據(jù)
        idx := idx + 255; -- 更新索引
    end loop;

    dbms_lob.freetemporary(utf8_data); -- 釋放臨時(shí)CLOB空間

exception
    when utl_http.end_of_body then
        utl_http.end_response(resp); -- 異常處理:如果到達(dá)響應(yīng)體結(jié)尾,確保關(guān)閉HTTP響應(yīng)
    when others then
        dbms_output.put_line('在調(diào)用Web服務(wù)時(shí)發(fā)生錯(cuò)誤: ' || sqlerrm); -- 其他異常處理
        if dbms_lob.istemporary(utf8_data) = 1 then
		傳參的中文會(huì)亂碼,但是方法內(nèi)部的中文不會(huì)亂碼
            dbms_lob.freetemporary(utf8_data); -- 釋放臨時(shí)CLOB空間
        end if;
end;
/

重要參數(shù)說明

上述程序已經(jīng)解決了中文亂碼的問題,但是還是不太完美,傳參的中文會(huì)亂碼,但是方法內(nèi)部的中文不會(huì)亂碼。

在這里插入圖片描述

Web Service的URL地址

url varchar2(4000) := 'http://10.xx.xx.xx:8085/WebService.asmx';

構(gòu)造SOAP請(qǐng)求包體

soap_env := 
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">' ||
'<soap:Header/>' ||            -- SOAP頭,這里為空
'<soap:Body>' ||               -- SOAP主體開始
'  <tem:testProcedure>' ||     -- 調(diào)用Web Service的方法名
'    <tem:sInput>' ||          -- 方法參數(shù)開始
' testConnect + 中文test' ||   -- 參數(shù)值,包括英文和中文字符
'</tem:sInput>' ||             -- 方法參數(shù)結(jié)束
'  </tem:testProcedure>' ||    -- 方法調(diào)用結(jié)束
'</soap:Body>' ||              -- SOAP主體結(jié)束
'</soap:Envelope>';            -- SOAP包體結(jié)束

其中testProcedure為webservice中定義的測試方法名,sInput為方法的參數(shù),多個(gè)參數(shù),自行添加。標(biāo)簽<tem:sInput></tem:sInput>中間填寫這個(gè)參數(shù)傳的實(shí)際值,其余部分無需修改。

構(gòu)造SOAP請(qǐng)求包體方法

我使用了soapui這個(gè)工具,怎么使用可以參考博客SoapUI 測試WebService接口可用性

依次如下操作即可:

在這里插入圖片描述

如果需要把上面的功能變成function或者procedure,請(qǐng)自行搜索相關(guān)的方法實(shí)現(xiàn)即可。

到此這篇關(guān)于Oracle通過procedure調(diào)用webservice接口的全過程的文章就介紹到這了,更多相關(guān)Oracle procedure調(diào)用webservice內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論