Oracle 使用UTL_HTTP发送http请求

Get方式:

CREATE OR REPLACE FUNCTION FN_HTTP_GET (v_url  VARCHAR2)
RETURN VARCHAR2
AS
BEGIN
  DECLARE
  req UTL_HTTP.REQ;
  resp UTL_HTTP.RESP;
  v_line VARCHAR2 ( 4000 );
  v_text VARCHAR2 ( 4000 );
  BEGIN
    v_text := '';
    BEGIN
      req := UTL_HTTP.BEGIN_REQUEST ( url => v_url, method => 'GET' );
      UTL_HTTP.SET_BODY_CHARSET('UTF-8');
      UTL_HTTP.SET_HEADER(req, 'Content-Type', 'application/x-www-form-urlencoded');
      resp := UTL_HTTP.GET_RESPONSE ( req );
      LOOP
      UTL_HTTP.READ_LINE ( resp, v_line, TRUE );
      v_text := v_text || v_line;
      END LOOP;
      UTL_HTTP.END_RESPONSE( resp );
      UTL_HTTP.END_REQUEST( req );
      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;
    return v_text;
  END;
END;

Oracle 使用UTL_HTTP发送http请求_第1张图片

Post方式:

CREATE OR REPLACE FUNCTION FN_HTTP_POST (v_url  VARCHAR2, v_body  VARCHAR2, v_body_type VARCHAR2)
RETURN VARCHAR2
AS
BEGIN
	DECLARE
	req UTL_HTTP.REQ;
	resp UTL_HTTP.RESP;
	v_line VARCHAR2 ( 4000 );
	v_text VARCHAR2 ( 4000 );
	BEGIN
		v_text := '';
		BEGIN
			req := UTL_HTTP.BEGIN_REQUEST ( url => v_url, method => 'POST' );
			UTL_HTTP.SET_BODY_CHARSET('UTF-8');
			UTL_HTTP.SET_HEADER(req, 'Content-Type', v_body_type);
			utl_http.set_header(req, 'Content-Length',lengthb(v_body));
		  utl_http.write_text(req, v_body);
      resp := UTL_HTTP.GET_RESPONSE ( req );      
      LOOP
      UTL_HTTP.READ_LINE ( resp, v_line, TRUE );
      v_text := v_text || v_line;
      END LOOP;

      UTL_HTTP.END_RESPONSE( resp );
      UTL_HTTP.END_REQUEST( req );
      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;
    return v_text;
  END;
END;

你可能感兴趣的:(oracle)