記一次使用utl_http方法調用接口,報字符或值錯誤


背景:ebs系統和其他系統通過utl_http包調用接口,使用log方法記錄日志。

某次調用接口,執行到記錄日志行報字符或值錯誤。

查找原因,發現是p_str的長度超過的32767的限制。

解決辦法:

 PROCEDURE log(p_str VARCHAR2) IS
  BEGIN
    fnd_file.put_line(fnd_file.log, p_str);
    dbms_output.put_line(p_str);
  END;
--解決l_messge_clob長度超過32767導致的溢出問題,字符或值錯誤
FOR i IN 0 .. trunc((dbms_lob.getlength(l_messge_clob) - 1) / l_step) LOOP
  log(dbms_lob.substr(l_messge_clob, l_step, i * l_step + 1));
END LOOP;


--同樣 utl_http.write_text 長度不能超過32767,

buffer       VARCHAR2(2000);
offset       NUMBER := 1;
amount       NUMBER := 1024;


--解決l_messge_clob長度超過3276導致的溢出問題,字符或值錯誤
---utl_http.write_text(l_req, p_content);
UTL_HTTP.SET_BODY_CHARSET('UTF-8');
WHILE (offset < v_req_length) LOOP
  dbms_lob.read(p_content, amount, offset, buffer);
  utl_http.write_text(r => l_req, data => buffer);
  offset := offset + amount;
END LOOP;

 


DECLARE
  v_doc_fin    CLOB := '';
  req          utl_http.req;
  res          utl_http.resp;
  url          VARCHAR2(1900) := 'url xxx';
  v_value      VARCHAR2(4000);
  vchunkdata   VARCHAR2(2000);
  v_req_length NUMBER;
  buffer       VARCHAR2(32767);
  offset       NUMBER := 1;
  amount       NUMBER := 32767;
  utl_err      VARCHAR2(1000);
BEGIN
  IF v_doc_fin IS NOT NULL THEN
    --v_doc_fin is JSON DOC of CLOB data type from a procedure
    v_req_length := dbms_lob.getlength(v_doc_fin);
    dbms_output.put_line(v_req_length);
    req := utl_http.begin_request(url, 'POST', 'HTTP/1.1');
    utl_http.set_header(req, 'Content-Length', v_req_length);
    utl_http.set_header(req, 'user-agent', 'mozilla/4.0');
    utl_http.set_header(req,
                        'content-type',
                        'application/json;charset=UTF-8');
    utl_http.set_header(req, 'Transfer-Encoding', 'chunked');
    utl_http.set_body_charset('UTF-8');
    WHILE (offset < v_req_length) LOOP
      dbms_lob.read(v_doc_fin, amount, offset, buffer);
      utl_http.write_text(r => req, data => buffer);
      offset := offset + amount;
    END LOOP;
    res := utl_http.get_response(req);
    BEGIN
      LOOP
        utl_http.read_line(res, v_value);
        dbms_output.put_line(v_value);
      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);
      WHEN OTHERS THEN
        dbms_output.put_line(utl_http.get_detailed_sqlerrm);
        dbms_output.put_line(dbms_utility.format_error_stack);
        dbms_output.put_line(dbms_utility.format_error_backtrace);
        dbms_output.put_line(dbms_utility.format_call_stack);
    END;
  END IF;
EXCEPTION
  WHEN OTHERS THEN
    utl_http.end_response(res);
    utl_err := utl_http.get_detailed_sqlerrm;
END;

 

 

 

參考:

Oracle 通過UTL_HTTP 發送http請求並處理發送內容中包含空格和特殊字符的問題

utl_http request and response stops when  > 32k

UTTL_HTTP to POST CLOB request

http://www.orafaq.com/forum/t/202946/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM