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)文章
Oracle組件實(shí)現(xiàn)動(dòng)態(tài)Web數(shù)據(jù)庫
Oracle組件實(shí)現(xiàn)動(dòng)態(tài)Web數(shù)據(jù)庫...2007-03-03解決Windows 7下安裝Oracle 11g相關(guān)問題的方法
本文將為大家介紹Windows 7下安裝Oracle 11g方面的有關(guān)問題解決方案。希望通過本文,能讓大家對(duì)11g這款產(chǎn)品有更多的認(rèn)識(shí),需要的朋友可以參考下2015-08-08修改計(jì)算機(jī)名或IP后Oracle10g服務(wù)無法啟動(dòng)的解決方法
修改計(jì)算機(jī)名或IP后Oracle10g無法啟動(dòng)服務(wù)即windows服務(wù)中有一項(xiàng)oracle服務(wù)啟動(dòng)不了,報(bào)錯(cuò),下面是具體的解決方法2014-01-01Oracle通過遞歸查詢父子兄弟節(jié)點(diǎn)方法示例
這篇文章主要給大家介紹了關(guān)于Oracle如何通過遞歸查詢父子兄弟節(jié)點(diǎn)的相關(guān)資料,遞歸查詢對(duì)各位程序員來說應(yīng)該都不陌生,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Oracle undo_management參數(shù)不一致錯(cuò)誤
因RAC的undo_management參數(shù)不一致導(dǎo)致Oracle數(shù)據(jù)庫mount報(bào)ORA-01105 ORA-01606錯(cuò)誤,本文就這個(gè)問題2013-11-11linux oracle數(shù)據(jù)庫刪除操作指南
本文將詳細(xì)介紹Linux操作系統(tǒng)下完全刪除Oracle數(shù)據(jù)庫的操作方法,需要的朋友可以參考下2012-11-11