Web Services 尝试从PL SQL调用Web服务时出现问题

t2a7ltrp  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(175)

我有一个返回JSON格式数据的Web服务(超过4M个字符)。我想将该数据检索到Oracle表中。
这是我在PL SQL中与Web服务通信的过程:

CREATE OR REPLACE PROCEDURE SGS_CRM.SP_REST_POZIV AS 
  req utl_http.req;
  res utl_http.resp;
  url varchar2(32767) := 'http://10.200.24.60/facebook_app/posts/list/';
  name varchar2(32767);
  buffer CLOB;--varchar2(32767); 
  content varchar2(32767);     
begin
  req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
  utl_http.set_header(req, 'user-agent', 'mozilla/4.0'); 
  utl_http.set_header(req, 'content-type', 'application/json'); 
  utl_http.set_header(req, 'Content-Length', length(content));
  UTL_HTTP.set_header ( req, 'Transfer-Encoding', 'chunked' );
  UTL_HTTP.SET_BODY_CHARSET('UTF-8');

  --utl_http.write_text(req, content);
  res := utl_http.get_response(req);
  -- process the response from the HTTP call
  begin

    loop
      utl_http.read_line(res, buffer, TRUE);
      dbms_output.put_line(buffer);
    end loop;
    utl_http.end_response(res);

  exception      
    when utl_http.end_of_body 
    then
      utl_http.end_response(res);
    when utl_http.too_many_requests 
    then
        utl_http.end_response(res);         
  end;

end SP_REST_POZIV;

这是我得到的错误:

[Error] Execution (1: 1): ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1491
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SGS_CRM.SP_REST_POZIV", line 24
ORA-06512: at line 2

当我将json数据限制为少于4000个字符时,我得到的响应如下:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">    
<html><head>    
<title>400 Bad Request</title>    
</head><body>  
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 10.200.24.60 Port 80</address>
</body></html>
{"id":"1","page_id":"199186763567391","message":"Kupujete stan? Pro\u0161le godine smo odobrili oko 25% vi\u0161e stambenih kredita nego prethodne, a ove godine nastavljamo istim trendom!\n\nNe znate \u0161ta vam je slede\u0107i korak? Iskoristite povoljan trenutak i prijavite se za besplatne konsultacije sa na\u0161im stru\u010dnjacima: http:\/\/stambeni-krediti.societegenerale.rs\/","link":"http:\/\/www.b92.net\/biz\/pr\/pr.php?nav_category=1244&yyyy=2017&nav_id=1250334","permalink_url":"https:\/\/www.facebook.com\/societegenerale.rs\/posts\/812151465604248","created_time":"2017-04-18 18:00:18","type":"link","name":"Ve\u0107a potra\u017enja za stambenim kreditima u pro\u0161loj godini - B92.net","post_id":"199186763567391_812151465604248","shares":"1","likes":"33","userlike_id":"435025440181160","userlike_name":"Zorica Stevanovic"}

所以我的问题是:
1.为什么我收到400错误请求错误?
1.为什么我不能把整个json拉出来?

eufgjt7s

eufgjt7s1#

我在过去使用过以下代码来处理SOAP请求。

DECLARE
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
        <m:CXMLTYPE-POSTInput xmlns:m="http://xmlns.oracle.com/orawsv/SOAPTEST/POST">
            <m:P_REQUEST-XMLTYPE-IN><Request>4</Request></m:P_REQUEST-XMLTYPE-IN>
        </m:CXMLTYPE-POSTInput>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>');
  V_SOAP_REQUEST_TEXT CLOB := V_SOAP_REQUEST.getClobVal();
  V_REQUEST           UTL_HTTP.REQ;
  V_RESPONSE          UTL_HTTP.RESP;
  V_BUFFER            VARCHAR2(32000);
  V_RESPONSE_TEXT     CLOB;
  V_RESPONSE_XML      XMLTYPE;
  V_WSDL              XMLTYPE;
BEGIN
    DBMS_LOB.CREATETEMPORARY(V_RESPONSE_TEXT, TRUE);

  begin
    V_REQUEST := UTL_HTTP.BEGIN_REQUEST(URL => :URL, METHOD => 'POST');
    UTL_HTTP.SET_HEADER(V_REQUEST, 'User-Agent', 'Mozilla/4.0');
    V_REQUEST.METHOD := 'POST';
    UTL_HTTP.SET_HEADER (R => V_REQUEST, NAME => 'Content-Length', VALUE => DBMS_LOB.GETLENGTH(V_SOAP_REQUEST_TEXT));
    UTL_HTTP.WRITE_TEXT (R => V_REQUEST, DATA => V_SOAP_REQUEST_TEXT);
    V_RESPONSE := UTL_HTTP.GET_RESPONSE(V_REQUEST); 
     LOOP
      UTL_HTTP.READ_LINE(V_RESPONSE, V_BUFFER, TRUE);
      if (LENGTH(V_BUFFER) > 0) then
        DBMS_LOB.WRITEAPPEND(V_RESPONSE_TEXT,LENGTH(V_BUFFER),V_BUFFER);   
      end if;
     END LOOP;
     UTL_HTTP.END_RESPONSE(V_RESPONSE);
  EXCEPTION
    WHEN UTL_HTTP.END_OF_BODY THEN
      UTL_HTTP.END_RESPONSE(V_RESPONSE);
  END;

  V_RESPONSE_XML := XMLTYPE(V_RESPONSE_TEXT);

  select XMLQUERY
         (
           'declare namespace SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"; (::)
            declare namespace SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"; (::)
            declare namespace xsi="http://www.w3.org/2001/XMLSchema-instance"; (::)
            declare namespace xsd="http://www.w3.org/2001/XMLSchema"; (::)
            declare namespace m="http://xmlns.oracle.com/orawsv/WSDLTEST/TEST_CLOB"; (::)
            $resp/SOAP-ENV:Envelope/SOAP-ENV:Body/*'
            passing V_RESPONSE_XML as "resp" returning content
         )
    into V_WSDL
    from DUAL;

  select V_WSDL.getClobVal()
    into :RESULT
    from dual;

    DBMS_LOB.FREETEMPORARY(V_RESPONSE_TEXT);

END;
/

也许你可以改变它来满足你的需要。

9rygscc1

9rygscc12#

对于超过32K的数据,我也得到了同样的错误。将“buffer”类型更改为clob在我的11.2.0.3.0数据库中没有任何效果。但是更改

utl_http.read_line(res, buffer, TRUE);

utl_http.read_text(res, buffer, 4000);

是解决这个问题的方法。

相关问题