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

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;

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

-- 刘轶鹤

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值