【问题标题】:problems while trying to call web service from PL SQL尝试从 PL SQL 调用 Web 服务时出现问题
【发布时间】:2017-04-23 11:49:05
【问题描述】:

我有一个以 json 格式(超过 4M 个字符)返回数据的 Web 服务。 我想将该数据检索到 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 Bad Request 错误?
  2. 为什么我不能提取整个 json?

【问题讨论】:

    标签: php json oracle web-services plsql


    【解决方案1】:

    过去我使用以下方法处理 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;
    /
    

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

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2011-02-18
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 2020-04-02
      相关资源
      最近更新 更多