2011-10-27 47 views
0

我想實現一個非常簡單的PL/SQL過程能夠從網上獲得xml文件。PL/SQL utl_http得到xml

PL/SQL代碼爲以下之一:

SET serveroutput ON SIZE 40000 
set escape on 

create or replace procedure testProc as 

url VARCHAR2(256) := 'http://www.mypage.com/testXML.xml'; 

req sys.utl_http.req; 
resp sys.utl_http.resp; 
txt VARCHAR2(100); 

begin 
    req := sys.utl_http.begin_request(url,'GET','HTTP/1.0'); 
    utl_http.set_header(req, 'content-type', 'text/xml;charset=UTF-8'); 
    utl_http.set_header(req, 'content-length', length(txt)); 
    resp := sys.utl_http.get_response(req); 

    LOOP 
    sys.utl_http.read_line(resp, txt, TRUE); 
    dbms_output.put_line(txt); 
    END LOOP; 
    sys.utl_http.end_response(resp); 

EXCEPTION WHEN sys.utl_http.end_of_body THEN 
sys.utl_http.end_response(resp); 

end testProc; 
/

我成功地構建了程序通過的SQLPlus。 然而,當我試着執行它時,我得到以下錯誤:

SQL> exec testProc; 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>413 Request Entity Too Large</title> 
</head><body> 
<h1>Request Entity Too Large</h1> 
The requested resource<br />/trentad/testXML.xml<br /> 
does not allow request data with GET requests, or the amount of data provided in 
the request exceeds the capacity limit. 
</body></html> 

PL/SQL procedure successfully completed. 

這很奇怪,因爲我想讀取XML文件是以下之一:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<note> 
    <body>Don't forget me this weekend!</body> 
</note> 

所用的相同代碼,沒有_ set_header_ functions正常工作正常HTML,提供正確的源頁面,請有人可以解釋爲什麼它不適用於一個簡單的xml文件?

回答

2

爲什麼在GET請求中設置content-type和content-length?您的GET請求不能有任何主體(請求實體)。這不是一個POST。

utl_http.set_header(req, 'content-type', 'text/xml;charset=UTF-8'); 
utl_http.set_header(req, 'content-length', length(txt)); 

您必須爲此做出響應。

+0

好吧,是的,你是對的。但是,如何設置響應頭?也許這是一個愚蠢的問題,但我沒有找到任何文檔或教程來處理這個用例。 – Raffaello

+0

'http://www.mypage.com/testXML.xml'上的頁面將發送響應並設置響應頭。你的PL/SQL代碼只是發送請求。我認爲你不必在PL/SQL代碼中設置響應頭。 – lkuty

0

從lkuty回答的問題看來,您正在查找Accept標題。這會通知服務器您將接受哪種類型的響應。