加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Oracle使用utl_http传送大于32kb数据解决方案

(2024-07-19 17:05:33)
标签:

utl_http

传送大于32kb数据

transfer-encoding

chunked

分类: 技术类
Oracle  使用utl_http 传送大于32kb数据解决方案
UTL_HTTP Post Data larger than 32KB


Question: How To Post Data larger than 32KB To a Url Using UTL_HTTP?
Is there a way to post Data larger than 32KB using utl_http.write_text?
Answer: Yes, You can post Data larger than 32KB to a Web Service (URL) using UTL_HTTP.
You would have to set header parameter “Transfer-Encoding” as a “chunked”
and then send your data in chunks.

如果BODY 内容超过32KB header需要增加
    utl_http.set_header(utl_req
                       ,'Transfer-Encoding'
                       ,'chunked');
示例


CREATE OR REPLACE FUNCTION xx_utl_http(p_url          VARCHAR2
                                      ,p_request_body CLOB) RETURN VARCHAR2 AS
  utl_req       utl_http.req;
  utl_resp      utl_http.resp;
  req_length    BINARY_INTEGER;
  response_body CLOB;
  resp_length   BINARY_INTEGER;
  buffer        VARCHAR2(2000);
  amount        PLS_INTEGER := 2000;
  offset        PLS_INTEGER := 1;
BEGIN
  utl_req := utl_http.begin_request(p_url
                                   ,'post'
                                   ,'http / 1.1');
  utl_http.set_header(utl_req
                     ,'Content-Type'
                     ,'application/json;charset=utf-8');
  req_length := dbms_lob.getlength(p_request_body);
  -- IF message data UNDER 32kb LIMIT 
  IF req_length <= 32767 THEN
    utl_http.set_header(utl_req
                       ,'Content-Length'
                       ,req_length);
    utl_http.write_text(utl_req
                       ,p_request_body);
    -- IF message data more than 32kb 
  ELSIF req_length > 32767 THEN
    utl_http.set_header(utl_req
                       ,'Transfer-Encoding'
                       ,'chunked');
    WHILE (offset < req_length)
    LOOP
      dbms_lob.read(p_request_body
                   ,amount
                   ,offset
                   ,buffer);
      utl_http.write_text(utl_req
                         ,buffer);
      offset := offset + amount;
    END LOOP;
  END IF;
  utl_resp := utl_http.get_response(utl_req);
  utl_http.read_text(utl_resp
                    ,response_body
                    ,32767);
  utl_http.end_response(utl_resp);
  RETURN response_body;
END;

提示需要注意 字符长度超长



-- 刘轶鹤

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有