分享

记一次使用utl_http方法调用接口,报字符或值错误

 安素暖 2021-07-29

背景: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;

参考:
https://blog.csdn.net/rznice/article/details/72625680
http://www./forum/t/202946/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多