kinjouj.github.io

utl_http

2010-02-02T00:00:00+09:00 PL/SQL

PL/SQLでインターネット上のHTMLを取得するメモ

declare
    url varchar2(100) := "http://d.hatena.ne.jp/surge";
    req utl_http.rep;
    res utl_http.resp;
    rec varchar2(1000);
    ft utl_http.file_type;
    ip varchar2(15);;
    rh varchar2(200);

begin
    ip := utl_inaddr.get_host_address();
    rh := utl_inaddr.get_host_name(ip);
    req := utl_http.begin_request(utl_uri.escape(url));

    utl_http.set_header(req, "User-Agent", "Mozilla/4.0");
    res := utl_http.get_response(req);
    ft := utl_file.fopen("C:/", "res.html", "W", 3000);

    loop
        utl_http.read_line(res, rec, true);
        utl_file.put_line(ft, rec);
    end loop;

    utl_http.end_response(res);
    utl_file.fclose(ft) exception
    when UTL_HTTP.END_OF_BODY then
        utl_http.end_response(res);
        utl_file.fclose(ft);
end;