oracle使用utl_http包发送post请求

使用plsq发送post或get请求,开始发送中文时有乱码,网上搜了半天,终于找到解决办法,关键点见红色字体部分,记录以备忘:
set serveroutput on;
exec dbms_output.enable(1000000000);
set escape '\'
DECLARE
  req   UTL_HTTP.REQ;
  resp  UTL_HTTP.RESP;
  value VARCHAR2(1024);  -- URL to post to
   v_url VARCHAR2(200) := 'http://ip:port/xxx';
  -- Post Parameters
  v_param VARCHAR2(500) := 'para1=xxx\?2=xxxx\?3=xxxx';
  v_param_length NUMBER := LENGTHB(v_param);
BEGIN
  -- Set up proxy servers if required
  --  UTL_HTTP.SET_PROXY('proxy.my-company.com', 'corp.my-company.com');
  req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST');
  --  UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
  UTL_HTTP.SET_BODY_CHARSET('UTF-8');
  UTL_HTTP.SET_HEADER (r      =>  req,
                       name   =>  'Content-Type',
                       value  =>  'application/x-www-form-urlencoded');
  UTL_HTTP.SET_HEADER (r      =>   req,
                       name   =>   'Content-Length',
                       value  =>   v_param_length);
  UTL_HTTP.WRITE_RAW (r    => req,
                    data => UTL_RAW.CAST_TO_RAW(v_param)); 
  resp := UTL_HTTP.GET_RESPONSE(req);
  LOOP
    UTL_HTTP.READ_LINE(resp, value, TRUE);
    DBMS_OUTPUT.PUT_LINE(value);
  END LOOP;
  UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
  WHEN UTL_HTTP.END_OF_BODY THEN
    UTL_HTTP.END_RESPONSE(resp);
END;
/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-2085171/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10972173/viewspace-2085171/

你可能感兴趣的:(oracle使用utl_http包发送post请求)