http - 为什么 UTL_HTTP.GET_RESPONSE 抛出 HTTP 协议(protocol)错误

标签 http plsql oracle11g

我正在使用 UTL_HTTP 从 Oracle 11g 调用网络服务。我确信我的证书和钱包设置正确,可以通过 HTTPS 进行连接。对于有效的业务数据,调用始终有效。

当我传递无效数据(在本例中为不存在的用户 ID)时,对 UTL_HTTP.GET_RESPONSE 的调用会引发异常:

ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1369
ORA-29263: HTTP protocol error

我没有得到返回的 UTL_HTTP.RESP 对象来调查任何 HTTP 错误代码或内容。许多其他无效数据用例已返回带有错误代码的响应,但这些都没有抛出异常。我能够处理 HTTP 错误代码的响应并从响应正文中获取错误消息。

我在抛出异常的特定情况下注意到的是:

  • 响应主体比其他情况大; ~2600 字节。
  • 响应正文包含 & 符号(这是一个 HTML 响应,其中包含转义的 XML 内容。我无法控制服务器响应)

我通过 curl 调用发现了这些。这些情况中的任何一个都可能是 UTL_HTTP.GET_RESPONSE 引发 HTTP 协议(protocol)错误的原因吗?

谢谢。

最佳答案

看来 utl_http 引发的错误是故意不透明的,除非您要求 Oracle 更明确一点。

要在引发异常时使错误消息更详细,请在 session 调用的某处:

utl_http.set_response_error_check(true)

否则,根据 utl_http 包本身的文档,您可以在异常处理代码中利用以下方法获取错误:

utl_http.get_detailed_sqlcode
utl_http.get_detailed_sqlerrm

至于处理大型请求,如果这是您的真正问题,将响应分块并将其累积在 clob 中可能会解决您的问题:

function get_request(p_url varchar2, p_payload_text varchar2)
return clob
is
    v_req utl_http.req;
    v_resp utl_http.resp;
    v_req varchar2(32767);
    v_resp clob;
    v_resp_chunked varchar2(32767);
    v_xml_resp xmltype;

begin

        utl_http.set_response_error_check(true);

        v_req := utl_http.begin_request(
            url => p_url
            , method => 'POST'
            , http_version => 'HTTP/1.1'
        );
        utl_http.set_body_charset(v_req, 'UTF-8');
        utl_http.set_persistent_conn_support(false, 0);

        utl_http.set_header(v_req, 'Content-Type', 'text/xml;charset=UTF-8');
        utl_http.set_header(v_req, 'Content-Length', length(p_payload_text));


        utl_http.write_text(v_req, p_payload_text);

        v_resp := utl_http.get_response(v_req);

        dbms_output.put_line(v_resp_chunked);

        dbms_lob.createtemporary(v_resp,true, dbms_lob.session);

        begin
            loop
                utl_http.read_text(v_resp, v_resp_chunked, 32767);
                --dbms_output.put_line(v_resp_chunked);
                dbms_lob.append(v_resp, v_resp_chunked);
            end loop;

            exception
            when utl_http.end_of_body or UTL_HTTP.TOO_MANY_REQUESTS then
                utl_http.end_response(v_resp);
                dbms_output.put_line('mess:' ||SQLERRM);
        end;

        dbms_lob.freetemporary(v_resp);

        return v_resp;
end;

关于http - 为什么 UTL_HTTP.GET_RESPONSE 抛出 HTTP 协议(protocol)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875610/

相关文章:

rest - 从 Lua 调用 Orthanc 的 REST Api 返回 "URI Badly Formatted"错误

angularjs - 如何将默认端点添加到 Angular $http 请求之前?

angularjs $http post 不适用于 Chrome 浏览器

使用 NVARCHAR2 hibernate

Java HTTP 客户端输出空 JSON

sql - PL/SQL 触发器问题

oracle - 存储过程未返回正确结果

c# - 轮询 Oracle 数据库以获取更改

c++ - 我哪里错了?设置日期的数据缓冲区

oracle11g - 代表 :50125:rwbuilder. conf :java. lang.NullPointerException