使用Oracle PL / SQL中的restful webservices获取数据

时间:2016-08-01 10:38:12

标签: oracle web-services rest plsql restful-architecture

我想从oracle数据库从webservices获取数据。 但我正在寻找它,不幸的是我无能为力。 请给我一些提示。我想用pl / sql代码中的用户名和密码访问它。 我该怎么做? 谢谢..

2 个答案:

答案 0 :(得分:1)

您可能需要查看UTL_HTTP package

答案 1 :(得分:0)

我像下面的代码一样解决它。我认为它会很有用。

declare 
  t_http_req     utl_http.req;
  t_http_resp    utl_http.resp;
  t_request_body varchar2(30000);
  t_respond      varchar2(30000);
  t_start_pos    integer := 1;
  t_output       varchar2(2000);

begin
  /*Construct the information you want to send to the webservice.
    Normally this would be in a xml structure. But for a REST-
  webservice this is not mandatory. The webservice i needed to
  call excepts plain test.*/
  t_request_body := 'the data you want to send to the webservice';

  /*Telling Oracle where the webservice can be found, what kind of request is made
    and the version of the HTTP*/
  t_http_req:= utl_http.begin_request( 'http://urlofwebservice'
                                     , 'GET'
                                     , 'HTTP/1.1');

  /*In my case the webservice used authentication with a username an password
    that was provided to me. You can skip this line if it's a public webservice.*/
  utl_http.set_authentication(t_http_req,'username','password');                                     

  /*Describe in the request-header what kind of data is send*/
  utl_http.set_header(t_http_req, 'Content-Type', 'text/xml charset=UTF-8');

  /*Describe in the request-header the lengt of the data*/
  utl_http.set_header(t_http_req, 'Content-Length', length(t_request_body));

  /*Put the data in de body of the request*/
  utl_http.write_text(t_http_req, t_request_body);


  /*make the actual request to the webservice en catch the responce in a
    variable*/
  t_http_resp:= utl_http.get_response(t_http_req);

  /*Read the body of the response, so you can find out if the information was
    received ok by the webservice.
    Go to the documentation of the webservice for what kind of responce you
    should expect. In my case it was:
  <responce>
    <status>ok</status>
  </responce>
  */
  utl_http.read_text(t_http_resp, t_respond);


         dbms_output.put_line(t_respond);


  /*Some closing?1 Releasing some memory, i think....*/
  utl_http.end_response(t_http_resp);
end;