Oracle 通过UTL_HTTP 发送http请求并处理发送内容中包含空格和特殊字符的问题

前端之家收集整理的这篇文章主要介绍了Oracle 通过UTL_HTTP 发送http请求并处理发送内容中包含空格和特殊字符的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
 
  • 1
  • 2
Oracle中可以通过UTL_HTTP发送http请求,例子如下:
2
  
  
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • CREATE OR REPLACE PROCEDURE test(v_userid in varchar2,v_content in varchar2 ) IS req UTL_HTTP.REQ; resp UTL_HTTP.RESP; v_message varchar2(10000); xmlstr varchar2(30000); begin begin req := UTL_HTTP.BEGIN_REQUEST('http://ip:port/weixin?userid='||v_userid||'&'||'content='||v_content); utl_http.set_header(req,'Content-Type',0)">'text/html; charset=utf-8'); utl_http.write_text(req,xmlstr); --通过body发送消息 xmlstr:=v_content; utl_http.set_header(req,0)">'Content-Length',lengthb(xmlstr)); utl_http.write_text(req,xmlstr); resp := UTL_HTTP.GET_RESPONSE(req); LOOP UTL_HTTP.read_line(resp,v_message,TRUE); dbms_output.put_line(v_message); END LOOP; utl_http.end_request(req); utl_http.end_response(resp); EXCEPTION WHEN utl_http.end_of_body THEN utl_http.end_response(resp); WHEN OTHERS THEN utl_http.end_response(resp); utl_http.end_request(req); end; END test;
    2
     
     调用的时候,如果v_content中包含空格或者其他特殊字符的时候请求就会失败,要想解决这个问题的话,就需要对内容进行编码,我们可以通过url encode来对相应的内容编码,web应用接收到相应的数据做相应的解码即可,修改后的代码如下:
    'content='||utl_url.escape(v_content,136)">true,0)">'UTF8'));
          utl_http.set_header(req,xmlstr); --通过body发送消息
          xmlstr:=utl_url.escape(v_content,0)">'UTF8'); 
          utl_http.set_header(req,136)">END test;

    猜你在找的Oracle相关文章